Add new column to data.frame through loop in R

血红的双手。 提交于 2021-02-11 12:39:17

问题


I have n number of data.frame i would like to add column to all data.frame

a <- data.frame(1:4,5:8) 
b <- data.frame(1:4, 5:8)
test=ls()

for (j in test){
  j = cbind(get(j),IssueType=j)
}

Problem that i'm running into is

j = cbind(get(j),IssueType=j)

because it assigns all the data to j instead of a, b.


回答1:


As commented, it's mostly better to keep related data in a list structure. If you already have the data.frames in your global environment and you want to get them into a list, you can use:

dflist <- Filter(is.data.frame, as.list(.GlobalEnv))

This is from here and makes sure that you only get data.frame objects from your global environment.

You will notice that you now already have a named list:

> dflist
# $a
#   X1.4 X5.8
# 1    1    5
# 2    2    6
# 3    3    7
# 4    4    8
# 
# $b
#   X1.4 X5.8
# 1    1    5
# 2    2    6
# 3    3    7
# 4    4    8

So you can easily select the data you want by typing for example

dflist[["a"]]

If you still want to create extra columns, you could do it like this:

dflist <- Map(function(df, x) {df$IssueType <- x; df}, dflist, names(dflist))

Now, each data.frame in dflist has a new column called IssueType:

> dflist
# $a
#   X1.4 X5.8 IssueType
# 1    1    5         a
# 2    2    6         a
# 3    3    7         a
# 4    4    8         a
# 
# $b
#   X1.4 X5.8 IssueType
# 1    1    5         b
# 2    2    6         b
# 3    3    7         b
# 4    4    8         b

In the future, you can create the data inside a list from the beginning, i.e.

dflist <- list(
  a = data.frame(1:4,5:8) 
  b = data.frame(1:4, 5:8)
)



回答2:


To create a list of your data.frames do this:

a <- data.frame(1:4,5:8); b <- data.frame(1:4, 5:8); test <- list(a,b)

This allows you to us the lapply function to perform whatever you like to do with each of the dataframes, eg:

out <- lapply(test, function(x) cbind(j))

For most data.frame operations I recommend using the packages dplyr and tidyr.




回答3:


wooo wooo

here is answer for the issue helped by @docendo discimus

Created Dataframe

a <- data.frame(1:4,5:8) b <- data.frame(1:4, 5:8)

Group data.frame into list

dflist <- Filter(is.data.frame, as.list(.GlobalEnv))

Add's extra column

dflist <- Map(function(df, x) {df$IssueType <- x; df}, dflist, names(dflist))

unstinting the data frame

list2env(dflist ,.GlobalEnv)



来源:https://stackoverflow.com/questions/36946822/add-new-column-to-data-frame-through-loop-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!