Reputation: 322
I want to add multiple empty columns to multiple dataframes. I know the code to do this for 1 dataframe is df[,namevector] <- NA
(other question). Namevector is a vector which contains the names of the empty variables that should be added. I have a list of multiple dataframes so I thought the following code would do the trick.
a <- data.frame(x = 1:10, y = 21:30)
b <- data.frame(x = 1:10, y = 31:40)
c <- list(a,b)
namevector <- c("z","w")
EmptyVariables <- function(df) {df[,namevector] <- NA}
sapply(X = c, FUN = EmptyVariables)
I don't get an error message, but these 2 lines of code also don't add the empty columns.
Upvotes: 0
Views: 971
Reputation: 15907
In principle the solution is there in the comments from BondedDust, but maybe some additional explanations might help.
Why did your original code not work? There are two things to be said about this:
EmptyVariables
is done in the environment of the function. Thus, only a local copy of the data frame df
is changed, but not the df
that exists in the global environment. Calling EmtpyVariables(a)
leaves a
unchanged.EmptyVariables
is an assignment, and since assignments don't return anything in R, also the function does not return anything. This is the reason that you simply get NA
twice from your call to sapply
. The solution to this has already been pointed out by BondedDust: the function body should be {df[,namevector] <- NA;df}
. In this case, the changed data frame is returned as the result of the function.Also a comment regarding sapply
: This function tries to return a vector or matrix. But your list of data frames can not reasonably be simplified in this way and you should therefore use lapply
.
Finally, this is the code that should do what you want:
EmptyVariables <- function(df) {df[,namevector] <- NA;df}
res <- lapply(X = c, FUN = EmptyVariables)
res
will be a list containing two data frames. Thus, res[[1]]
and res[[2]]
will give you a
and b
with the empty columns added, respectively.
Upvotes: 1