Christina
Christina

Reputation: 73

R: Calculating mean() from a list

I have cut of a frame from a data.set. picture

The problem is, that I can not calculate the mean Value of Var1, as it is probably not numeric.

How do I proceed?

mean(c1[, "Var1"])
mean(c1$Var1)

Doesn't work...

> c1["Var1"]
        Var1
116   661574
128   671194
331   847073
454   933425
652  1113353
761  1220950
764  1223786
978  1580029
1150 1987981
1367 2900735
1380 2976310
1383 3002309
1404 3149761
1408 3178648
1439 3431430
1488 3754229
1506 3910297
> mean(c1[, "Var1"])
[1] NA

Upvotes: 2

Views: 15034

Answers (2)

wolf_wue
wolf_wue

Reputation: 296

so your numbers are factors.

try

mean(as.numeric(levels(c1$Var1)))

Upvotes: 2

learningtocode
learningtocode

Reputation: 48

The datatype of the data in the column is numeric and the mean of the column that you want will also have a the datatype numeric. The error you are getting: In mean.default(c1["var1"]) : argument is not numeric or logical: returning NA. It means that the output you are getting when you use Single square braces [] is not numeric. Single square braces always return data with the same data type which in this case would be a list(). But what you want is mean() with numeric datatype. Try mean(c1[["var1"]]) The better way to do this would be to use column indexes. Hope this helps!

Example.

Upvotes: 2

Related Questions