我有一个数据框如下
DATA <- read.table(text = "col1 col2
ATG AT
NA AT1
ATC AT2
ATD AT3
ATE AT3
ATF AT4
ATH AT4
ATI AT4
ATL AT4", header=TRUE)
我用过
result<-data.frame(table(DATA$col2))
得到我想要的东西
##result
col1 col2
1 AT
1 AT1
1 AT2
2 AT3
4 AT4
问题是col1中的第二行有NA,应该算作零,但我无法做到。它应该是
##result
col1 col2
1 AT
0 AT1
1 AT2
2 AT3
4 AT4
答案 0 :(得分:2)
dfs = read.table(text =
"col1 col2
ATG AT
NA AT1
ATC AT2
ATD AT3
ATE AT3
ATF AT4
ATH AT4
ATI AT4
ATL AT4", sep = " ", header = TRUE)
data.frame(xtabs( !is.na(col1) ~ col2, data = dfs))
答案 1 :(得分:0)
如何稍微更改代码:
.hidden-md-up