主要问题:有没有办法用数据框名称填充 df,而不仅仅是手动输入并粘贴它?
我在一个文件夹中有 20 个 csv 文件,其中有一个看起来像这样的数据网格。文件 1:
X1 | X2 | X3 | |
---|---|---|---|
Y1 | 1 | 2 | 3 |
Y2 | 4 | 5 | 6 |
Y3 | 7 | 8 | 9 |
文件2:
X1 | X2 | X3 | |
---|---|---|---|
Y1 | 1 | 4 | 7 |
Y2 | 2 | 5 | 8 |
Y3 | 3 | 6 | 9 |
注意:X1,2,3 和 Y1,2,3 是坐标,填充的值只是示例值,不代表任何模式
每个文件都有一个唯一的 ID,例如,US_plot_1.csv、US_plot_2.csv、UK_plot_1.csv、US_plot_2.csv,
我想填充一个 df 将这些文件排序到 R 可以分析的列中,按文件名分组,即,
文件名 | 十 | 和 | 价值观 |
---|---|---|---|
US_plot_1 | X1 | Y1 | 1 |
US_plot_1 | X1 | Y2 | 4 |
US_plot_1 | X1 | Y3 | 7 |
US_plot_1 | X2 | Y1 | 2 |
US_plot_1 | X2 | Y2 | 5 |
US_plot_1 | X2 | Y3 | 8 |
US_plot_1 | X3 | Y1 | 3 |
US_plot_1 | X3 | Y2 | 6 |
US_plot_1 | X3 | Y3 | 9 |
US_plot_2 | X1 | Y1 | 1 |
US_plot_2 | X1 | Y2 | 2 |
US_plot_2 | X1 | Y3 | 3 |
我理解填充数据我可以循环它。
df<- lapply(Sys.glob("*.csv"), read.csv) #to load all the csvs
df<- as.data.frame(df)
df<-lapply(split(sequence(ncol(df)), rep(1:(4/4), each = 4)), function(x) df[, x])
filenames<- list.files(path=getwd()) #to get the filenames
filenames2<- substr(filenames, 1, 9)
for (i in 1:20) {
assign(paste(filenames2[i], i), data.frame(df[[i]]))
}
然后,当我需要将每个数据更改为 r-analysable df 时,我可以使用带有 gather()的tidyr包:
US_plot_1<-DF %>% gather (X_coord, Value, X1:X3)
我知道我总是可以诉诸于手动粘贴它使用
US_plot_1$filename<-paste("US_plot_1")
但我想知道是否有更有效的方法?
您可以在lapply
命令中执行此操作:
library(dplyr)
library(tidyr)
filenames <- Sys.glob("*.csv")
df<- lapply(filenames, function(x) {
#Read the csv
read.csv(x) %>%
#Get the data in long format, gather is retired using pivot_longer
pivot_longer(cols = starts_with('X')) %>%
#Adding a new column as filename
mutate(filename = tools::file_path_sans_ext(x))
})
如果您希望它们作为单独的数据框:
names(df) <- tools::file_path_sans_ext(filenames)
list2env(df, .GlobalEnv)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句