-3

我有一个大数据框架。我必须逐列计算以 _xxsgt 结尾的单词出现了多少次。

  Col1             Col2  
  a               54_xxsgt   
  123_xxsgt       e     
  d               f  
  429_s_xxsgt     g 

期望的输出:

Col1:2(发生 123_xxsgt 和 429_s_xxsgt)
Col2:1(发生 54_xxsgt)...。

4

2 回答 2

4
# First, a reproducible example)
set.seed(42)
dd <- sample(letters[1:20], 100, replace = TRUE)
ix <- as.character(sample(c("", "_xxsgt"), 100, replace = TRUE))
dd <- paste(dd, ix, sep="")
df <- as.data.frame(matrix(dd, ncol=10))

# solution
sapply(df, function(x) length(grep("_xxsgt", x)))
V1  V2  V3  V4  V5  V6  V7  V8  V9 V10 
 6   7   6   9   4   5   4   5   6   4 
于 2013-01-18T14:22:21.260 回答
3

试试这个:

> DF <- read.table(text=" Col1             Col2  
    a               54_xxsgt   
    123_xxsgt       e     
    d               f  
    429_s_xxsgt     g ", header=T)
> 
> apply(DF, 2, function(x) sum(grepl('_xxsgt', x)))
Col1 Col2 
   2    1 
于 2013-01-18T14:25:11.833 回答