循环遍历R中的数据帧列表

时间:2021-09-08 18:34:38

I have a series of data frames, df1 df2, where each data frame follow this structure:

我有一系列数据帧,df1 df2,其中每个数据帧都遵循以下结构:

x <- c(1:5)
y <- c(1:5)
df1 <- data.frame("Row One"=x, "Row Two"=y)

Sample output for df1:

df1的示例输出:

 Row.One    Row.Two
    1           1
    2           2
    3           3
    4           4
    5           5

I put each data frame into a list dfList <- list(df1,df2...)

我把每个数据框放到一个列表dfList < - list(df1,df2 ...)

Now I want to loop through each data frame object in this list to replace the column names using this command:

现在,我想循环遍历此列表中的每个数据框对象,以使用此命令替换列名:

a <- grep("One", colnames(df))
b <- grep("Two", colnames(df))

names(df)[a] <- "R1"
names(df)[b] <- "R2"

How can I structure a loop in R so that I no matter how many data frames are in the list object the column name changing commands above will be applied to each data frame?

如何构建R中的循环,以便无论列表对象中有多少数据帧,上面的列名更改命令都将应用于每个数据帧?

3 个解决方案

#1


9  

> df1 <- data.frame("Row One"=x, "Row Two"=y)
> df2 <- data.frame("Row Two"=y,"Row One"=x)
> dfList <- list(df1,df2)
> lapply(dfList, function(x) {
                    names(x)[ grep("One", names(x))] <- "R1"
                    names(x)[ grep("Two", names(x))] <- "R2"
                    x} )
[[1]]
  R1 R2
1  1  1
2  2  2
3  3  3
4  4  4
5  5  5

[[2]]
  R2 R1
1  1  1
2  2  2
3  3  3
4  4  4
5  5  5

#2


4  

Just use length(dfList)?

只需使用长度(dfList)?

for(i in 1:length(dfList))
{
    a <- grep("One", names(dfList[[i]]))
    ... #etc.
}

Using lapply will be faster.

使用lapply会更快。

ChangeNames = function(Data)
{
    a = grep("One", names(Data))
    b = grep("Two", names(Data))
    names(Data)[c(a,b)] <- c("R1", "R2")
    return(Data)
}
lapply(dfList, ChangeNames) #Returns list of renamed data frames.

#3


3  

Or use llply (from plyr) or lapply like so:

或者使用llply(来自plyr)或lapply如下:

library(plyr)
result_list <- llply(list_of_df, function(x) {
                # do the replacing
                return(x)
                })

#1


9  

> df1 <- data.frame("Row One"=x, "Row Two"=y)
> df2 <- data.frame("Row Two"=y,"Row One"=x)
> dfList <- list(df1,df2)
> lapply(dfList, function(x) {
                    names(x)[ grep("One", names(x))] <- "R1"
                    names(x)[ grep("Two", names(x))] <- "R2"
                    x} )
[[1]]
  R1 R2
1  1  1
2  2  2
3  3  3
4  4  4
5  5  5

[[2]]
  R2 R1
1  1  1
2  2  2
3  3  3
4  4  4
5  5  5

#2


4  

Just use length(dfList)?

只需使用长度(dfList)?

for(i in 1:length(dfList))
{
    a <- grep("One", names(dfList[[i]]))
    ... #etc.
}

Using lapply will be faster.

使用lapply会更快。

ChangeNames = function(Data)
{
    a = grep("One", names(Data))
    b = grep("Two", names(Data))
    names(Data)[c(a,b)] <- c("R1", "R2")
    return(Data)
}
lapply(dfList, ChangeNames) #Returns list of renamed data frames.

#3


3  

Or use llply (from plyr) or lapply like so:

或者使用llply(来自plyr)或lapply如下:

library(plyr)
result_list <- llply(list_of_df, function(x) {
                # do the replacing
                return(x)
                })