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.

4
  • 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 Commented Apr 29, 2016 at 20:26
  • can you help me with a sample code Commented Apr 29, 2016 at 20:27
  • Yes. What is the new column IssueType supposed to hold? simply a and b (the names of the data.frames)? Commented Apr 29, 2016 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 Commented Apr 29, 2016 at 20:30

3 Answers 3

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)
)
Sign up to request clarification or add additional context in comments.

6 Comments

> 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
@PraveenRKaruppannan, that's what I demonstrated in my answer.. did you check it?
this does the job like charm, but now how to I split them up
@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 stackoverflow.com/a/17697397/3521006
I have to regroup them into single dataset which need to be pushed into DB
|
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.

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.