Reputation: 101
I am simply looking to standardise my set of data frame variables to a 100 point scale. The original variables were on a 10 point scale with 4 decimal points.
I can see that my error is not unheard of e.g
Why am I getting a function error in seemingly similar R code?
Error: only defined on a data frame with all numeric variables with ddply on large dataset
but I have verified that all variables are numeric using
library(foreign)
library(scales)
ches <- read.csv("chesshort15.csv", header = TRUE)
ches2 <- ches[1:244, 3:10]
rescale(ches2, to = c(0,100), from = range(ches2, na.rm = TRUE, finite = TRUE))
This gives the error: Error in FUN(X[[i]], ...) : only defined on a data frame with all numeric variables
I have verified that all variables are of type numeric using str(ches2)
- see below:
'data.frame': 244 obs. of 8 variables:
$ galtan : num 8.8 9 9.65 8.62 8 ...
$ civlib_laworder : num 8.5 8.6 9.56 8.79 8.56 ...
$ sociallifestyle : num 8.89 7.2 9.65 9.21 8.25 ...
$ immigrate_policy : num 9.89 9.6 9.38 9.43 9.13 ...
$ multiculturalism : num 9.9 9.6 9.57 8.77 9.07 ...
$ ethnic_minorities : num 8.8 9.6 9.87 9 8.93 ...
$ nationalism : num 9.4 10 9.82 9 8.81 ...
$ antielite_salience: num 8 9 9.47 8.88 8.38
In short, I'm stumped as to why it refuses to carry out the code.
For info, Head(bb)
gives :
galtan civlib_laworder sociallifestyle immigrate_policy multiculturalism ethnic_minorities
1 8.800 8.500 8.889 9.889 9.900 8.800
2 9.000 8.600 7.200 9.600 9.600 9.600
3 9.647 9.563 9.647 9.375 9.571 9.867
4 8.625 8.786 9.214 9.429 8.769 9.000
5 8.000 8.563 8.250 9.133 9.071 8.929
6 7.455 8.357 7.923 8.800 7.800 8.455
nationalism antielite_salience
1 9.400 8.000
2 10.000 9.000
3 9.824 9.471
4 9.000 8.882
5 8.813 8.375
6 8.000 8.824
Upvotes: 2
Views: 2514
Reputation: 1888
The rescale
function is throwing that error because it expects a numeric vector, and you are feeding it a dataframe instead. You need to iterate; go through every column on your dataframe and scale them individually.
Try this:
sapply(ches2, rescale, to = c(0,100))
You don't need the range(ches2, na.rm = TRUE, finite = TRUE)
portion of your code because rescale is smart enough to remove NA values on its own
Upvotes: 3