-3

I have a big data.frame. I have to count how many times a word ending with _xxsgt occurs, column by column.

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

Desired output:

Col1: 2 (123_xxsgt and 429_s_xxsgt occur)
Col2: 1 (54_xxsgt occur) ....

halfer
  • 19,824
  • 17
  • 99
  • 186
Bfu38
  • 1,081
  • 1
  • 8
  • 17

2 Answers2

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 
Arun
  • 116,683
  • 26
  • 284
  • 387
3

Try this:

> 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 
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
  • Hi Jiber, sorry for the late answer. I followed you suggestion that was ok for me. Thank you a lot! – Bfu38 Jan 29 '13 at 13:14