在R中合并多个表
编辑:我理解之前有人问过合并多个数据帧的问题,但为了更简洁,我一直在讨论如何合并多个表而不首先转换为数据帧。如果您想了解如何合并多个数据帧,请参阅伟大的答案(也链接到下面) 所以我已经有了在R中合并多个数据帧的方法,但我希望有人能帮我找到一种更优雅的方法。下面是一个代码的例子,我有工作。鉴于df1、df2和df3是具有相同列(包括列名“class”)但行数不同的数据帧,我可以:在R中合并多个表,r,merge,R,Merge,编辑:我理解之前有人问过合并多个数据帧的问题,但为了更简洁,我一直在讨论如何合并多个表而不首先转换为数据帧。如果您想了解如何合并多个数据帧,请参阅伟大的答案(也链接到下面) 所以我已经有了在R中合并多个数据帧的方法,但我希望有人能帮我找到一种更优雅的方法。下面是一个代码的例子,我有工作。鉴于df1、df2和df3是具有相同列(包括列名“class”)但行数不同的数据帧,我可以: table1 <- table(df1$class) table2 <- table(df2$class)
table1 <- table(df1$class)
table2 <- table(df2$class)
table3 <- table(df3$class)
table1我们需要添加by=“Var1”
参数来合并:
# dummy data
transcript <- rep(c("a","b","c","d","e","f"))
family <- rep(c("L1","L2","ERV"),2)
class <- rep(c("LINE","LINE","LTR"),2)
df1 <- data.frame(transcript, family, class)
# get table as data.frame
table1 <- as.data.frame(table(df1$class))
table2 <- as.data.frame(table(df1$class))
table3 <- as.data.frame(table(df1$class))
# merge without by
Reduce(function(...) merge(..., all = TRUE),
list(table1, table2, table3))
# Var1 Freq
# 1 LINE 4
# 2 LTR 2
# merge with by = "Var1"
Reduce(function(...) merge(..., all = TRUE, by = "Var1"),
list(table1, table2, table3))
# Var1 Freq.x Freq.y Freq
# 1 LINE 4 4 4
# 2 LTR 2 2 2
#虚拟数据
成绩单太好了!by=“Var1”
技巧非常有效。你能把它作为一个答案让我接受吗?中的by
语句也存在于dupe本身中。。。在我看来,只是没有仔细阅读答案enough@DavidArenburg哎呀,那就让我们把它当傻瓜关上吧。
table1 <- as.data.frame(table(df1$class))
colnames(table1) <- c("ID","counts1")
table2 <- as.data.frame(table(df2$class))
colnames(table2) <- c("ID","counts2")
table3 <- as.data.frame(table(df3$class))
colnames(table3) <- c("ID","counts3")
transcript <- rep(c("a","b","c","d","e","f"))
family <- rep(c("L1","L2","ERV"),2)
class <- rep(c("LINE","LINE","LTR"),2)
df1 <- data.frame(transcript, family, class)
transcript family class
a L1 LINE
b L2 LINE
c ERV LTR
d L1 LINE
e L2 LINE
f ERV LTR
# dummy data
transcript <- rep(c("a","b","c","d","e","f"))
family <- rep(c("L1","L2","ERV"),2)
class <- rep(c("LINE","LINE","LTR"),2)
df1 <- data.frame(transcript, family, class)
# get table as data.frame
table1 <- as.data.frame(table(df1$class))
table2 <- as.data.frame(table(df1$class))
table3 <- as.data.frame(table(df1$class))
# merge without by
Reduce(function(...) merge(..., all = TRUE),
list(table1, table2, table3))
# Var1 Freq
# 1 LINE 4
# 2 LTR 2
# merge with by = "Var1"
Reduce(function(...) merge(..., all = TRUE, by = "Var1"),
list(table1, table2, table3))
# Var1 Freq.x Freq.y Freq
# 1 LINE 4 4 4
# 2 LTR 2 2 2