我有一个数据框的列表,每个数据框都有一个“名称”列和一个“示例”列。如何合并单个列的数据框列表?
df1:
Name Sample1
A 23
B 445
C 456
df2:
Name Sample2
A 45
B 984
C 374
我怎么能合并所有dataframes在我的列表中,这样他们最终像这样:从类似的问题
merged:
Name Sample1 Sample2
A 23 45
B 445 984
C 456 374
我试图回答的SO,但他们都没有产生预期的结果。例如。 merged.data.frame = Reduce(function(...) merge(..., all=T), list.of.data.frames)
编辑:
我原来的剧本是下面列出
# List all files in current working directory
fs <- list.files()
# Load the data from each file into a list of data frames
dfs <- lapply(fs, read.table, header=TRUE, sep="\t")
# Select only the Name and Concentration columns from the list of data frames
dfs <- lapply(dfs, subset, select=c(Name, Concentration))
# Sort each dataframe alphabetically by the Name column
dfs <- lapply(dfs, function(df){df[order(df$Name),]})
# Rename each Concentration heading with the basename of the filename where the data originates
for (i in 1:length(dfs)){colnames(dfs[[i]])[2] <- substr(fs[i], 1, nchar(fs[i]) - 4)}
# Merge all the dataframes together by the Name column
# Write merged dataframe out to a tab-delimited file
write.table(dfs, ".", sep="\t")
尝试''从库(plyr)'join_all'。此外,'Reduce(函数(...)合并(...,all = TRUE,by =“Name”),list(df1,df2))'给出了基于2个数据集显示的预期输出。 – akrun 2014-10-06 16:44:12