R计算字符串的出现次数

时间:2015-10-06 17:55:29

标签: r

我已经使用x的xlsx包从多张xlsx表中读取数据。目前我的数据框如下所示

 firstcol                               SecondCol
 A                                       abcd
 B                                       bds
 A                                       <NA>
 A                                       asd
 C                                       <NA>
 B                                       adfdf
 ?                                       <NA>
 C                                       adfd

从上面的数据我想得到以下输出。

Firsrcol  FirstcolCount         SecondCol
A           3 times              2       # we'll not count NA's  
B           2 times              2
C           2 times              1
other       1 times              0

有没有直接的方法可以做到这一点?对此提出一些建议会很高兴。

2 个答案:

答案 0 :(得分:0)

一种 <dc.AlphaRelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <CheckBox android:id="@+id/approvedCheckBox" android:button="@drawable/checkbox_approve" android:clickable="false" local:MvxBind="Click ApprovedCommand" /> </dc.AlphaRelativeLayout> <dc.AlphaRelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <CheckBox android:id="@+id/rejectedCheckBox" android:button="@drawable/checkbox_reject" android:clickable="false" local:MvxBind="Click RejectedCommand" /> </dc.AlphaRelativeLayout> 方法:

data.table

答案 1 :(得分:-1)

虽然不太清楚你究竟是什么意思。像这样的东西?

library(dplyr)

df %>% group_by(firstcol) %>% summarise(FirstcolCount = n(), SecondCol = n() - sum(SecondCol == "<NA>"))
Source: local data frame [4 x 3]

  firstcol FirstcolCount SecondCol
1        ?             1         0
2        A             3         2
3        B             2         2
4        C             2         1
相关问题