1

I have 8 data frames and I want to create a variable for each of this data frame. I use a for a loop and the code I have used is given below:

year <- 2001
dflist <- list(bhps01, bhps02, bhps03, bhps04, bhps05, bhps06, bhps07, bhps08)

for (df in dflist){
df[["year"]] <- as.character(year)
assign()
year <- year + 1
}

bhps01,...,bhps08 are the data frame objects and year is a character variable. bhps01 is the data frame for year 2001, bhps02 is the data frame for year 2002 and so on.

Each data corresponds to a year, so bhps01 corresponds to year 2001, bhps corresponds to 2002 and so on. So, I want to create a year variable for each one of these data. So, year variable would be "2001" for bhps01 data, "2002" for bhps02 and so on.

The code runs fine but it does not create the variable year for either of the data frames except the local variable df.

Can someone please explain the error in the above code? Or is there an alternative of doing the same thing?

2 Answers 2

1

The syntax in the for loop is wrong. I am not entirely sure what you try to accomplish but let us try this

year = 2001 

A = data.frame(a = c(1, 1), b = c(2, 2))
B = data.frame(a = c(1, 1), b = c(2, 2))
L = list(A, B)

for (i in seq_along(L)) {
  L[[i]][, dim(L[[i]])[2] + 1] = as.character(rep(year,dim(L[[i]])[1]))  
  year = year + 1
}

with output

> L
[[1]]
  a b   V3
1 1 2 2001
2 1 2 2001

[[2]]
  a b   V3
1 1 2 2002
2 1 2 2002

That is what you intend as output, correct?

In order to change the column name to "year" you can do

L = lapply(L, function(x) {colnames(x)[3] = "year"; x})
Sign up to request clarification or add additional context in comments.

7 Comments

I have tried this with the following code: year = 2001 L <- list(bhps01, bhps02) for (i in seq_along(L)) { L[[i]][, dim(L[[i]])[1] + 1] = as.character(rep(year,dim(L[[i]])[1])) year = year + 1 } but it gives me the following error message: Error in [<-.data.frame(*tmp*, , dim(L[[i]])[1] + 1, value = c("2001", : new columns would leave holes after existing columns
I have 8 data frames namely, bhps01, bhps02, bhps03, bhps04, bhps05, bhps06, bhps07 and bhps08. Each data corresponds to a year, so bhps01 corresponds to 2001, bhps corresponds to 2002 and so on. So, I want to create a year variable for each one of these data. So, year variable would be "2001" for bhps01 data, "2002" for bhps02 and so on. I hope it is clear now?
Sorry, there was a typo! Try this year = 2001 L <- list(bhps01, bhps02) for (i in seq_along(L)) { L[[i]][, dim(L[[i]])[2] + 1] = as.character(rep(year,dim(L[[i]])[1])) year = year + 1 }
Hi the code runs this time, but still the variable "year" is not created for either of the data frame?
No, the variable is probably V3. You can rename it, though, with the command L = lapply(L, function(x) {colnames(x)[3] = "year"; x}). Does it work now?
|
0

You take a copy of the dataframe from the list, and add the variable "year" to it, but then do not assign it anywhere, which is why it is discarded (i.e. not stored in a variable). Here's a fix:

year <- 2001
dflist <- list(bhps01, bhps02, bhps03, bhps04, bhps05, bhps06, bhps07, bhps08)

counter <- 0
for (df in dflist){
  counter <- counter + 1
  df[["year"]] <- as.character(year)
  dflist[[counter]] <- df
  year <- year + 1
}

If you want the original dataframes to be edited, you could assign the result back on the rather then into the list. This is a bit of an indirect route, and notice the change in creating the dflist with names. We create the df, and then assign it to the original name. For example:

year <- 2001
dflist <- list(bhps01 = bhps01, bhps02 = bhps02, bhps03 = bhps03, bhps04 = bhps04, bhps05 = bhps05, bhps06 = bhps06, bhps07 = bhps07, bhps08 = bhps08)

counter <- 0
for (df in dflist){
  counter <- counter + 1
  df[["year"]] <- as.character(year)
  dflist[[counter]] <- df
  assign(names(dflist)[counter], df)
  year <- year + 1
}

3 Comments

I have tried this code. The code runs fine, but the variable year is not created in the either of these dataframes.
That is because when you create a dataframe using bhps01, 02 etc., the changes are now in the list you created (dflist). If you want to have each of the individual dataframes to have the year variable, you might just assign them back. After the loop finishes: bhps01 <- dflist[[1]] ; bhps02 <- dflist[[2]] etc.
How can you automate bhps01 <- dflist[[1]] ; bhps02 <- dflist[[2]] etc. using loop or inside the same loop body?

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.