0

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.

talat
  • 68,970
  • 21
  • 126
  • 157
Praveen DA
  • 358
  • 4
  • 17
  • The standard way in R would be to keep all related data.frames in a `list` and use `lapply` to to loop over all of them and create new columns – talat Apr 29 '16 at 20:26
  • can you help me with a sample code – Praveen DA Apr 29 '16 at 20:27
  • Yes. What is the new column IssueType supposed to hold? simply `a` and `b` (the names of the data.frames)? – talat Apr 29 '16 at 20:28
  • Yup, I am trying to put the data frame name as new column > a X1.4 X5.8 Issu 1 1 5 a 2 2 6 a 3 3 7 a 4 4 8 a something like thid – Praveen DA Apr 29 '16 at 20:30

3 Answers3

3

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)
)
Community
  • 1
  • 1
talat
  • 68,970
  • 21
  • 126
  • 157
  • > a X1.4 X5.8 1 1 5 2 2 6 3 3 7 4 4 8 I need to add new column which represent data.frame name Sample output that i am looking for > a X1.4 X5.8 Issu 1 1 5 a 2 2 6 a 3 3 7 a 4 4 8 a – Praveen DA Apr 29 '16 at 20:52
  • @PraveenRKaruppannan, that's what I demonstrated in my answer.. did you check it? – talat Apr 29 '16 at 20:54
  • this does the job like charm, but now how to I split them up – Praveen DA Apr 29 '16 at 21:02
  • 1
    @PraveenRKaruppannan, why would you want to? Just keep them sitting in a list. As I said, that would be the R way to do things. If you insist on putting them back separately in the global environment, check http://stackoverflow.com/a/17697397/3521006 – talat Apr 29 '16 at 21:04
  • I have to regroup them into single dataset which need to be pushed into DB – Praveen DA Apr 29 '16 at 21:14
  • @ docendo discimus,the link you did the job i was looking for .thanks a lot I will add the complete code below I will help someone like me – Praveen DA Apr 29 '16 at 21:18
0

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.

Axel
  • 150
  • 2
  • 7
  • > a X1.4 X5.8 1 1 5 2 2 6 3 3 7 4 4 8 I need to add new column which represent data.frame name Sample output that i am looking for > a X1.4 X5.8 Issu 1 1 5 a 2 2 6 a 3 3 7 a 4 4 8 a – Praveen DA Apr 29 '16 at 20:51
0

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)

Praveen DA
  • 358
  • 4
  • 17