我有一些表可以统计三个数据帧中眼睛颜色的出现频率。我以为我可以将这些表转换为数据框,然后将它们合并为新的数据框。我相信错误是来自以下事实:我将表转换为数据框,并且合并似乎增加了行数。但我需要:
Color Tb1 Tb2 Tb3
Bl 5 0 3
Blk 6 7 0
小的条件是,并非每个数据框都具有黑色或蓝色眼睛颜色。因此,我需要考虑到这一点,然后将NA更改为0。
我尝试过:
chart<-merge(tb1,tb2,tb3)
chart
Error in fix.by(by.x, x) :
'by' must specify one or more columns as numbers, names or logical
AND
chart<-merge(tb1,tb2,tb3,all=T)
chart
Error in fix.by(by.x, x) :
'by' must specify one or more columns as numbers, names or logical
示例代码:
one<-c('Black','Black','Black','Black','Black','Black','Blue','Blue','Blue','Blue','Blue')
two<-c('Black','Black','Black','Black','Black','Black','Black')
three<-c('Blue','Blue','Blue')
tb1<-table(one)
tb2<-table(two)
tb3<-table(three)
tb1<-as.data.frame(tb1)
tb2<-as.data.frame(tb2)
tb3<-as.data.frame(tb3)
答案 0 :(得分:2)
您可以使用bind_rows
软件包中的dplyr
将所有表直接转换为一个小块:
# creating the setup given in the question
one<-c('Black','Black','Black','Black','Black','Black','Blue','Blue','Blue','Blue','Blue')
two<-c('Black','Black','Black','Black','Black','Black','Black')
three<-c('Blue','Blue','Blue')
tb1<-table(one)
tb2<-table(two)
tb3<-table(three)
# note that there is no need for individual dataframes
# bind the rows of the given tables into a tibble
result <- dplyr::bind_rows(tb1, tb2, tb3)
# replace NAs with 0 values
result[is.na(result)] <- 0
# check the resulting tibble
result
# # A tibble: 3 x 2
# Black Blue
# <dbl> <dbl>
# 1 6 5
# 2 7 0
# 3 0 3
答案 1 :(得分:1)
按照自己的方式做,我将做以下事情(需要更正列名):
*maxVal = atof(line); //Set the minimum value
但是,为了更好的方法,请检查newframe <- merge(tb1, tb2, by.x ="one", by.y ="two", all = TRUE)
merge(newframe, tb3, by.x ="one", by.y ="three", all = TRUE)
联接。