简体   繁体   English

在 R 中将不同文件中的多列写入一个数据帧

[英]Writing multiple columns from different files into one data frame in R

I have the following files in the folder clust_rot_10_.csv,clust_rot_11_.csv,clust_rot_12_.csv,clust_rot_3_.csv,clust_rot_4_.csv,clust_rot_5_.csv,clust_rot_6_.csv,clust_rot_7_.csv,clust_rot_8_.csv,clust_rot_9_.csv,driver_rot_10_.csv,driver_rot_11_.csv,driver_rot_12_.csv,driver_rot_3_.csv,driver_rot_4_.csv,driver_rot_5_.csv,driver_rot_6_.csv,driver_rot_7_.csv,driver_rot_8_.csv,driver_rot_9_.Z628CB5675FF524F3E I have the following files in the folder clust_rot_10_.csv,clust_rot_11_.csv,clust_rot_12_.csv,clust_rot_3_.csv,clust_rot_4_.csv,clust_rot_5_.csv,clust_rot_6_.csv,clust_rot_7_.csv,clust_rot_8_.csv,clust_rot_9_.csv,driver_rot_10_. csv,driver_rot_11_.csv,driver_rot_12_.csv,driver_rot_3_.csv,driver_rot_4_.csv,driver_rot_5_.csv,driver_rot_6_.csv,driver_rot_7_.csv,driver_rot_8_.csv,driver_rot_9_.Z628CB5675FF524F3E 719B7AA2E88FE3FZ 719B7AA2E88FE3FZ

In the files that start with clust_rot_ , there are two columns X and Y. I need to retrieve the data from the Y column from all the files that start with clust_rot_ using an R program.在以clust_rot_开头的文件中,有两列 X 和 Y。我需要使用 R 程序从所有以clust_rot_开头的文件中检索 Y 列中的数据。 Please help with this请帮忙

The solution goes as follows:解决方案如下:

  1. Load the first file with read.table and store it in a data.frame x使用read.table加载第一个文件并将其存储在 data.frame x
  2. In a loop over all remaining files, read each file into a temporary data.frame (eg newdata ) and append it to the previously lodaed data with cbind(x, newdata)在所有剩余文件的循环中,将每个文件读入临时 data.frame (例如newdata )和 append 使用cbind(x, newdata)将其读入先前加载的数据

Example with the builtin dataset iris :内置数据集iris的示例:

x <- iris[,1:2]
newdata <- iris[,3:4]
x <- cbind(x, newdata)

Try this code -试试这个代码 -

#Get vector of filenames that has 'clust_rot_' in it. 
filenames <- list.files(pattern = 'clust_rot_', full.names = TRUE)
#From each file read the file and extract Y column from  it.
result <- as.data.frame(lapply(filenames, function(x) read.csv(x)$Y))
#Rename the columns with the name of the file
names(result) <- tools::file_path_sans_ext(basename(filenames))
#Write it as new csv
write.csv(result, 'result.csv', row.names = FALSE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过匹配第二个数据帧中的多列来查询一个R数据帧 - querying one R data frame by matching multiple columns from a second data frame R如何合并不同的数据帧列 - R how to merge different columns of data frame in one 通过R中的多个不同列对数据帧进行分组和汇总 - Grouping & summarizing data frame by multiple different columns in R 将包含多张工作表的多个 xlsx 文件读取到一个 R 数据框中 - Read multiple xlsx files with multiple sheets into one R data frame 将来自不同列但来自同一数据框的元素与 R 进行比较 - Comparing elements from different columns but from the same data frame with R 从R数据框中的多个列中提取数据,然后搜索另一个 - Extract data from multiple columns in R data frame, then searching another 如何合并R中来自同一数据帧的字符的不同列 - How to combine different columns of characters from the same data frame in R 计算来自不同数据帧的两列之间的R的相关性 - compute correlation in R between two columns from different data frame R - 从数据框中的不同列中删除值 - R - Remove values from different columns in a data frame 从一个数据帧中的多个.csv和cbind提取同一列(不同长度) - extract the same column (different length) from multiple .csv and cbind in one data frame - R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM