Praveen DA
Praveen DA

Reputation: 354

Add new column to data.frame through loop in R

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.

Upvotes: 0

Views: 6395

Answers (3)

Praveen DA
Praveen DA

Reputation: 354

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)

Upvotes: 0

talat
talat

Reputation: 70266

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)
)

Upvotes: 3

Axel
Axel

Reputation: 160

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.

Upvotes: 0

Related Questions