gpt4 book ai didi

r - 批量为变量分配名称时,R Functionals不起作用

转载 作者:行者123 更新时间:2023-12-02 08:05:22 25 4
gpt4 key购买 nike

我想使用功能来为csv数据文件批量分配名称,我已经测试了功能,包括Mapmapplylapply和功能assign来替代<-,但是所有这些功能都无法正常工作,只有传统的循环工作原理,任何人都可以解释一下?提前致谢!

这是我的代码:

allData <- c("Actinomyces.csv", "Bacteria.csv", "COD.csv", "Fungi.csv", "NH3.csv", "NO2.csv", "NO3.csv", "TN.csv", "TP.csv")
#CSV files in the working path
csvs <- lapply(allData, read.csv)
#Store data frames into a list
nameList <- c("Actinomyces", "Bacteria", "COD", "Fungi", "NH3", "NO2", "NO3", "TN", "TP")
lapply(c(1:9), function(i){assign(nameList[i], csvs[[i]])})
#Doesn't work
Map(assign, nameList, csvs)
#Doesn't work either
mapply(assign, nameList, csvs)
#Doesn't work again
for (i in 1:9){
assign(nameList[i], csvs[[i]])
}
#Only this works

最佳答案

这是两种基本的R方式:

junk <- Map("assign", nameList, csvs, MoreArgs = list(.GlobalEnv))

list2env(setNames(csvs, nameList), .GlobalEnv)

关于r - 批量为变量分配名称时,R Functionals不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52062125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com