-2

I have a dataframe

df
score
1                2291
2                3548
3                2595
4                3214
5                3275
6                 251
7                  47
8                  47
9                  47
10               2490

However when I do this before I take a mean:

as.numeric(unlist(df))
[1] 1 7 4 5 6 3 8 8 8 2

I am getting completely different numbers. Any thoughts? Thanks!

Badmiral
  • 1,549
  • 3
  • 35
  • 74

1 Answers1

1

Since the column you're operating on has class factor (per your comment), you'll get erroneous numbers when calling as.numeric() on it directly. To correct this, first change the column to class character, then to class numeric with

df$score <- as.numeric(as.character(df$score))

and you should be okay.

Furthermore, you can find the means of the numeric columns more easily with

colMeans(df)
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245