简体   繁体   中英

How to create a column on multiple dataframes R

I have 9 dataframes (data1,data2, ... data3) in which I want to make some simulations.I want to add to each dataframe a variable called castigoex that moves between 0 and 1. I'. having trouble adding this new column into each dataframe. Does anybody know how to do this? Thanks in advance!

simulations <- 100000
for (i in 1:9) {
  assign(paste("data",i, sep = ""), 
         lapply(mutate("castigoex" = as.numeric(runif(simulations, 
                                                      min = 0, max = 1)))
}

Try this:

# three toy dataframe
df1 <- data.frame(x = c(1:10))
df2 <- data.frame(x = c(11:20))
df3 <- data.frame(x = c(21:30))

# make a list
dfl <- list(df1, df2, df3)

# add "castigoex" to each df
for (i in 1:length(dfl)) {
  dfl[[i]]$castigoex <- runif(10, 0, 1)
}

# I advise you to keep the dataframes in the list, 
# but if you want to split them again
list2env(setNames(dfl, paste0("df", seq_along(dfl))), envir = parent.frame())

If you want to avoid for loop, you can use mapply

new_list <- mapply(function(x) "[<-"(x, "castigoex", value = runif(10, 0, 1)),
                   dfl, SIMPLIFY = FALSE)

# now you have old list and new list. To split the list (with new name for df)
list2env(setNames(new_list,paste0("df_new", seq_along(new_list))), 
         envir = parent.frame())

If you want to add more than one column, one possible solution is to use the lapply function

new_list2 <- lapply(dfl, function(x) cbind(x, castigoex = runif(10, 0, 1), 
                                           variable2 = runif(10, 0, 1)))

list2env(setNames(new_list2, paste0("df", seq_along(dfl))), envir = parent.frame())

I think the solution of Leonardo is the most complete, here is my contribution using lapply , cbind and keeping everything on a list.

#create data framw#
df1=data.frame(replicate(10,sample(0:50,1000,rep=TRUE)))
df2=data.frame(replicate(10,sample(0:50,1000,rep=TRUE)))
df3=data.frame(replicate(10,sample(0:50,1000,rep=TRUE)))
#join the list
dfss=list(df1,df2,df3)
#use lapply
dfss2=lapply(dfss, function(x) cbind(x, castigoex=runif(1000, max = 1, min = 0)) )

then you can split the list back as recommended above, although it might be better to keep them on a list because then you can use all the apply family of functions

A major issue is that you are not assigning the columns created with mutate back to the original dataframes.

You may do this by creating a list with all your dataframes, than using lapply()

You may be safe with:

#example dataframes

data1<-data.frame(1:100000, 100001:200000)
data2<-data.frame(1:100000, 100001:200000)
data3<-data.frame(1:100000, 100001:200000)

simulations <- 100000

##extract names of dataframes

data_names<-str_extract(ls(), '^data[[:digit:]]{1}$')[!is.na(str_extract(ls(), '^data[[:digit:]]{1}$'))]

#create list of dataframes

dataframes<-lapply(data_names, function(x)get(x))

#lapply function to add the desired simulations

lapply(dataframes, function(x){
        cbind(x, 'castigoex'=runif(simulations, min=0, max=1))
        })
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM