简体   繁体   English

lapply之后从列表返回到data.frame

[英]returning from list to data.frame after lapply

I have a very simply question about lapply. 我有一个关于lapply的非常简单的问题。 I am transitioning from STATA to R and I think there is some very basic concept that I am not getting about looping in R. But I have been reading about it all afternoon and can't figure out a reasonable way to do this very simple thing. 我正在从STATA过渡到R,我认为有一些非常基本的概念,我不会在R中循环。但是我整个下午都在阅读有关它的内容,无法找到一种合理的方法来完成这一非常简单的事情。

I have three data frames df1, df2, and df3 that all have the same column names, in the same order, etc. 我有三个数据帧df1,df2和df3,它们都具有相同的列名,相同的顺序等。

I want to rename their columns all at once. 我想一次重命名它们的列。

I put the data frames in a list: 我将数据帧放在列表中:

dflist <- list(df1, df2, df3)

What I want the new names to be: 我想要的新名称是:

varlist <- c("newname1", "newname2", "newname3")

Write a function that replaces names with those in varlist, and lapply it over the data frames 编写一个用varlist中的名称替换名称的函数,并将其套用到数据帧上

ChangeNames <- function(x) {
  names(x) <- varlist 
  return(x)
}

dflist <- lapply(dflist, ChangeNames)

So, as far as I understand, R has changed the names of the copies of the data frames that I put in the list, but not the original data frames themselves. 因此,据我了解,R更改了我放入列表中的数据帧副本的名称,但未更改原始数据帧本身。 I want the data frames themselves to be renamed, not the elements of the list (which are trapped in a list). 我希望重命名数据框本身,而不是重命名列表中的元素(被困在列表中)。

Now, I can go 现在,我可以走了

df1 <- as.data.frame(dflist[1])
df2 <- as.data.frame(dflist[2])
df2 <- as.data.frame(dflist[3])

But that seems weird. 但这似乎很奇怪。 You need a loop to get back the elements of a loop? 您需要一个循环来取回循环的元素吗?

Basically: once you've put some data frames in a list and run your function on them via lapply, how do you get them back out of the list, without starting back at square one? 基本上:一旦将一些数据框放入列表中并通过lapply在其上运行函数,如何将它们从列表中移出而又不从第一个平方开始?

If you just want to change the names, that isn't too hard in R. Bear in mind that the assignment operator, <- , can be applied in sequence. 如果只想更改名称,则在R中并不难。请记住,可以按顺序应用赋值运算符<- Hence: 因此:

names(df1) <- names(df2) <- names(df3) <- c("newname1", "newname2", "newname3")

I am not sure I understand correctly, do you want to rename the columns of the data frames or the components of the list that contain the data frames? 我不确定我是否理解正确,是否要重命名数据框的列或包含数据框的列表的组件?

If it is the first, please always search before asking, the question has been asked here . 如果是第一个,请始终在询问之前进行搜索, 此处已提出问题

So what you can easily do in case you have even more data frames in the list is: 因此,如果列表中有更多数据帧,您可以轻松地执行以下操作:

# Creating some sample data first
> dflist <- list(df1 = data.frame(a = 1:3, b = 2:4, c = 3:5),
+ df2 = data.frame(a = 4:6, b = 5:7, c = 6:8),
+ df3 = data.frame(a = 7:9, b = 8:10, c = 9:11))

# See how it looks like
> dflist
    $df1
  a b c
1 1 2 3
2 2 3 4
3 3 4 5

$df2
  a b c
1 4 5 6
2 5 6 7
3 6 7 8

$df3
  a  b  c
1 7  8  9
2 8  9 10
3 9 10 11

# And do the trick
> dflist <- lapply(dflist, setNames, nm = c("newname1", "newname2", "newname3"))

# See how it looks now
> dflist

$df1
  newname1 newname2 newname3
1        1        2        3
2        2        3        4
3        3        4        5

$df2
  newname1 newname2 newname3
1        4        5        6
2        5        6        7
3        6        7        8

$df3
  newname1 newname2 newname3
1        7        8        9
2        8        9       10
3        9       10       11

So the names were changed from a , b and c to newname1 , newname2 and newname3 for each data frame in the list. 因此,列表中每个数据框的名称都从abc更改为newname1newname2newname3

If it is the second, you can do this: 如果是第二个,则可以执行以下操作:

> names(dflist) <- c("newname1", "newname2", "newname3")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM