[R] Invalid Type of Char Argument When Sum() is Used

jim holtman jholtman at gmail.com
Fri Mar 13 10:31:15 CET 2009


Do an 'str(dat)' and see what the structure is.  When I read in your data I get:

> x
   V1        V2       V3
1 AAA 1000000.0 1222.312
2 AAC     100.2    0.330
> str(x)
'data.frame':   2 obs. of  3 variables:
 $ V1: Factor w/ 2 levels "AAA","AAC": 1 2
 $ V2: num  1000000 100
 $ V3: num  1222.31 0.33
> sum(x$V1)
Error in Summary.factor(1:2, na.rm = FALSE) :
  sum not meaningful for factors

> sum(x$V2)
[1] 1000100

So you 'V1" might be characters which is exactly what your error
message is telling you.

On Fri, Mar 13, 2009 at 4:37 AM, Gundala Viswanath <gundalav at gmail.com> wrote:
> Hi all,
>
> I have the following data
>
> AAA  1e+06 1222.312
> AAC  100.2  0.33
>
> However the following command
>
> dat <- read.table("mydat.txt");
>
> print(dat)
>
> n1 <- sum(as.matrix(dat$V1));
>
> gives:
> Error in sum(as.matrix(dat$V1)) : invalid 'type' (character) of argument
>
>
> What's wrong with the data type so that it gives such error?
>
>
> - Gundala Viswanath
> Jakarta - Indonesia
>
> ______________________________________________
> R-help at r-project.org 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 that you are trying to solve?




More information about the R-help mailing list