合并R中的表,如果同时合并两个单元格,则合并单元格

合并R中的表,如果同时合并两个单元格,则合并单元格,r,merge,R,Merge,您好,您能解释一下我如何合并两个表以生成一个饼图吗 #read input data dat = read.csv("/ramdisk/input.csv", header = TRUE, sep="\t") # pick needed columns and count the occurences of each entry df1 = table(dat[["C1"]]) df2 = table(dat[["C2"]]) # rename columns names(df1) <

您好,您能解释一下我如何合并两个表以生成一个饼图吗

#read input data
dat = read.csv("/ramdisk/input.csv", header = TRUE, sep="\t")

# pick needed columns and count the occurences of each entry
df1 = table(dat[["C1"]])
df2 = table(dat[["C2"]])

# rename columns
names(df1) <- c("ID", "a", "b", "c", "d")
names(df2) <- c("ID", "e", "f", "g", "h")

# show data for testing purpose
df1  
# ID   a   b   c   d 
#241  18  17  28  29 
df2
# ID   e   f   g   h 
#230  44   8  37  14 
# looks fine so far, now the problem:

# what I want to do ist merging df and df2 
# so that df will contain the overall numbers of each entry
# df should print
# ID   a   b   c   d    e   f   g   h 
#471  18  17  28  29   44   8  37  14 
# need them to make a nice piechart in the end
#pie(df) 
#读取输入数据
dat=read.csv(“/ramdisk/input.csv”,header=TRUE,sep=“\t”)
#选择所需的列并计算每个条目的出现次数
df1=表格(dat[[“C1”]]
df2=表格(dat[[“C2”]]
#重命名列

名称(df1)一种方法是
堆叠
,然后
rbind
,并进行
聚合

out <- aggregate(values ~ ., rbind(stack(df1), stack(df2)), sum)

或者另一种方法是连接表,然后使用
tapply
sum
进行分组

v1 <- c(df1, df2)
tapply(v1, names(v1), sum)
rowsum(v1, group = names(v1))

另一种方法是使用
data.table
colSums
中的
rbindlist
来获取总数
rbindlist
with
fill=TRUE
接受所有列,即使它们不在两个表中

df1<-read.table(text="ID   a   b   c   d 
241  18  17  28  29 ",header=TRUE)
df2<-read.table(text="ID   e   f   g   h 
230  44   8  37  14" ,header=TRUE)

library(data.table)
setDT(df1)
setDT(df2)
res <- rbindlist(list(df1,df2), use.names=TRUE, fill=TRUE)
colSums(res, na.rm=TRUE)

 ID   a   b   c   d   e   f   g   h 
471  18  17  28  29  44   8  37  14 
df1我编写了一个包,以直观的方式处理这类任务(我希望如此!)。您只需要在两个表之间有一个公共id(我们将使用
tibble::row_id_to_column
),然后您就可以合并并处理与
sum
的列冲突

使用@pierre lapointe的数据:

library(tibble)
# devtools::install_github("moodymudskipper/safejoin")
library(safejoin)

res <- safe_inner_join(rowid_to_column(df1),
                       rowid_to_column(df2),
                       by = "rowid",
                       conflict = sum)

res
#   rowid  ID  a  b  c  d  e f  g  h
# 1     1 471 18 17 28 29 44 8 37 14

只需
cbind(ID=sum(df1[1],df2[1]),df1[-1],df2[-1])
在您想要的结果中,对ID进行求和。两个表中至少需要一列才能合并。@PierreLapointe:这就是为什么我使用名称作为两个“ID”的名字。这是错误的吗?因为它是一个一维表格,所以使用
c
而不是
cbind
library(tibble)
# devtools::install_github("moodymudskipper/safejoin")
library(safejoin)

res <- safe_inner_join(rowid_to_column(df1),
                       rowid_to_column(df2),
                       by = "rowid",
                       conflict = sum)

res
#   rowid  ID  a  b  c  d  e f  g  h
# 1     1 471 18 17 28 29 44 8 37 14
pie(unlist(res[1,])[-(1:2)])