【问题标题】:How to create a column on multiple dataframes R如何在多个数据框 R 上创建列
【发布时间】:2021-02-03 21:43:28
【问题描述】:

我有 9 个数据帧(data1,data2,... data3),我想在其中进行一些模拟。我想在每个数据帧中添加一个名为 castigoex 的变量,该变量在 0 和 1 之间移动。我'。无法将此新列添加到每个数据框中。有人知道怎么做这个吗?提前致谢!

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

【问题讨论】:

    标签: r loops


    【解决方案1】:

    试试这个:

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

    如果要避免for循环,可以使用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())
    

    如果要添加多于一列,一种可能的解决方案是使用lapply 函数

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

    【讨论】:

    • 非常感谢!这完美地工作。我有一个问题,如果我想添加超过 1 个新变量,mapply 函数会怎样?
    • 我更改了答案,以便能够添加多个列
    【解决方案2】:

    我认为 Leonardo 的解决方案是最完整的,这是我使用lapplycbind 的贡献并将所有内容都列在一个列表中。

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

    然后您可以按照上面的建议将列表拆分回来,尽管将它们保留在列表中可能会更好,因为这样您就可以使用所有 apply 系列函数

    【讨论】:

      【解决方案3】:

      一个主要问题是您没有将使用 mutate 创建的列分配回原始数据帧。

      你可以通过创建一个包含所有数据框的列表来做到这一点,而不是使用 lapply()

      您可以放心:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-17
        • 1970-01-01
        • 1970-01-01
        • 2020-06-19
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多