I have a list in R that contains several data frames. I want to iterate over the data frames and calculate the min/max of a value from the data frame. Here's the code that I have right now:
我在R中有一个包含几个数据框的列表。我想迭代数据帧并计算数据帧中值的最小值/最大值。这是我现在的代码:
firstname = names(dats)[1]
xlim = c( min( dats[[firstname]][,xlab] ), max( dats[[firstname]][,xlab] ) )
for ( name in names(dats) ) {
xlim = c( min(xlim[1],dats[[name]][,xlab]), max(xlim[2],dats[[name]][,xlab]) )
}
This seems ugly to me, as it requires a lot of code to do something very simple. Is there a more canonical way to do this in R?
这对我来说似乎很难看,因为它需要很多代码来做一些非常简单的事情。在R中有更典型的方法吗?
3 个解决方案
#1
10
You can use lapply
to extract the xlab
column out of all the data-frames, and unlist
to combine into one vector, then take the min
or max
:
您可以使用lapply从所有数据帧中提取xlab列,并取消列表以组合成一个向量,然后取最小值或最大值:
xlab <- 'a'
dats <- list( df1 = data.frame(a=sample(1:3), b = sample(11:13)),
df2 = data.frame(a=sample(1:3), b = sample(11:13)))
> min( unlist( lapply( dats, '[', xlab ) ) )
[1] 1
> max( unlist( lapply( dats, '[', xlab ) ) )
[1] 3
#2
3
Can you combine the data frames from the list of data frames into one data frame? I would use the plyr
package and rbind.fill
, which would allow the data frames to have mismatched columns as long as the column of interest is named the same in all data frames.
您能否将数据帧列表中的数据帧合并为一个数据帧?我会使用plyr包和rbind.fill,这将允许数据帧具有不匹配的列,只要感兴趣的列在所有数据帧中被命名为相同。
library(plyr)
df.orig <- data.frame(one = rep(1:4, each = 4), two = 1:16)
df.list <- dlply(df.orig, "one")
df.new <- rbind.fill(df.list)
xlim <- with(df.new, c(min(two), max(two)))
#3
1
If I understand the question correctly, heres something to do with plyr:
如果我正确地理解了这个问题,那就是与plyr有关:
dats <- list( df1 = data.frame(a=sample(1:3), b = sample(11:13)),
df2 = data.frame(a=sample(1:3), b = sample(11:13)))
library(plyr)
xlim <- adply(do.call("rbind",dats),2,function(x)c(min(x),max(x)))
names(xlim)=c("xlab","min","max")
xlim
xlab min max
1 a 1 3
2 b 11 13
Gives per variable the minimum and maximum collapsed over all data frames in the list.
为每个变量提供列表中所有数据框的最小和最大折叠。
EDIT: shortened the code abit. I do assume that each dataframe contains the same number of columns in the same order.
编辑:缩短代码abit。我假设每个数据帧包含相同顺序的相同列数。
#1
10
You can use lapply
to extract the xlab
column out of all the data-frames, and unlist
to combine into one vector, then take the min
or max
:
您可以使用lapply从所有数据帧中提取xlab列,并取消列表以组合成一个向量,然后取最小值或最大值:
xlab <- 'a'
dats <- list( df1 = data.frame(a=sample(1:3), b = sample(11:13)),
df2 = data.frame(a=sample(1:3), b = sample(11:13)))
> min( unlist( lapply( dats, '[', xlab ) ) )
[1] 1
> max( unlist( lapply( dats, '[', xlab ) ) )
[1] 3
#2
3
Can you combine the data frames from the list of data frames into one data frame? I would use the plyr
package and rbind.fill
, which would allow the data frames to have mismatched columns as long as the column of interest is named the same in all data frames.
您能否将数据帧列表中的数据帧合并为一个数据帧?我会使用plyr包和rbind.fill,这将允许数据帧具有不匹配的列,只要感兴趣的列在所有数据帧中被命名为相同。
library(plyr)
df.orig <- data.frame(one = rep(1:4, each = 4), two = 1:16)
df.list <- dlply(df.orig, "one")
df.new <- rbind.fill(df.list)
xlim <- with(df.new, c(min(two), max(two)))
#3
1
If I understand the question correctly, heres something to do with plyr:
如果我正确地理解了这个问题,那就是与plyr有关:
dats <- list( df1 = data.frame(a=sample(1:3), b = sample(11:13)),
df2 = data.frame(a=sample(1:3), b = sample(11:13)))
library(plyr)
xlim <- adply(do.call("rbind",dats),2,function(x)c(min(x),max(x)))
names(xlim)=c("xlab","min","max")
xlim
xlab min max
1 a 1 3
2 b 11 13
Gives per variable the minimum and maximum collapsed over all data frames in the list.
为每个变量提供列表中所有数据框的最小和最大折叠。
EDIT: shortened the code abit. I do assume that each dataframe contains the same number of columns in the same order.
编辑:缩短代码abit。我假设每个数据帧包含相同顺序的相同列数。