Bob Green
2006-Aug-18 08:13 UTC
[R] using R to perform a word count - syntax refinement and incorrect number of dimensions error
Hello, 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) V3.PD V3.HD V3.LP 1 1 1 > sapply(paste(wc1[,1], wc1[,2]), nwords) Error in wc1[, 1] : incorrect number of dimensions > dim (wc1) NULL
jim holtman
2006-Aug-18 11:08 UTC
[R] using R to perform a word count - syntax refinement and incorrect number of dimensions error
'wc1' is a vector and not a matrix. The reference 'wc1[,1]' is not legal:> wc1 <-c("V3.PD", "V3.HD", "V3.LP") > dim(wc1)NULL> wc1[,1]Error in wc1[, 1] : incorrect number of dimensions>What is it that you are trying to do? On 8/18/06, Bob Green <bgreen at dyson.brisnet.org.au> wrote:> Hello, > > 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) > V3.PD V3.HD V3.LP > 1 1 1 > > sapply(paste(wc1[,1], wc1[,2]), nwords) > Error in wc1[, 1] : incorrect number of dimensions > > dim (wc1) > NULL > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?