[R] using R to perform a word count - syntax refinement and incorrect number of dimensions error

Bob Green bgreen at dyson.brisnet.org.au
Fri Aug 18 10:13:15 CEST 2006


I am hoping someone can advise me regarding an error message I received and 
if needed, refine some syntax. I am wanting to calculate the word count for 
each row of a dataframe. Below, I have 3 variables ("V3.PD", "V3.HD", 
"V3.LP") which I want to obtain a word count for, by each row which 
contains these variables.

Any assistance is much appreciated,

Bob Green

 > wordcount1 <- read.csv("c:\\newstext.csv",header=T)
 > attach(wordcount1)
 > names(wordcount1)
[1] "X"     "i"     "V3.PD" "V3.PG" "V3.HD" "V3.BY" "V3.SN" "V3.LP" "V3.TD"
 > wc1 <-c("V3.PD", "V3.HD", "V3.LP")
 > dim(wordcount1)
[1] 178   9
 > nwords <- function(x){
+ res <- strsplit(as.character(x), "[ \t]+")
+ res <- lapply(res, length)
+ unlist(res)
+ }
 > sapply(wc1, nwords)
     1     1     1
 > sapply(paste(wc1[,1], wc1[,2]), nwords)
Error in wc1[, 1] : incorrect number of dimensions
 > dim (wc1)

More information about the R-help mailing list