CLM
CLM

Reputation: 119

SUM on a data.frame returns error

I have a data.frame named PSIBL_out.

   sallgi     stext2    staxid           stext3  
1   hgd;te;     hgh     gfg;dgj;jhh       AJJC
2   hf          jhd     hjhj              hgfd
3   oui         dhk     kjll              jhjs

I want to find all rows in which the column "staxids" has multiple values, separated by ";". For each such row, I want to look in the the "sallgi" column, count how many times the ";" appears and put the number in "a".

The code I wrote is the following:

  for(i in PSIBl_out[grep(";", PSIBl_out[,"staxids"]),])
  {
   a <- sum(PSIBl_out[grep(";", PSIBl_out[,"sallgi"]),])
  }

I get the following error, which I believe is for the SUM function:

 Error in FUN(X[[1L]], ...) : 
  only defined on a data frame with all numeric variables

Ideas?

Upvotes: 1

Views: 167

Answers (1)

joran
joran

Reputation: 173577

Your question is still a bit unclear (and by the way, you should clarify your question by editing it, not leaving comments!).

But I'm guessing you want something like this:

library(stringr)
> dat <- read.table(text = "sallgi     stext2    staxid           stext3  
+ 1   hgd;te;     hgh     gfg;dgj;jhh       AJJC
+ 2   hf          jhd     hjhj              hgfd
+ 3   oui         dhk     kjll              jhjs",header = TRUE,sep = "")
> str_count(dat$sallgi[grepl(";",dat$staxid)],";")
[1] 2

But it is not clear from your question whether you want a count for each row in your original data frame, or just for those rows with ; present in the staxid column.

Upvotes: 1

Related Questions