What I have is a data frame that contains, among others, a factor field which holds a range of values used as factor. From what I understand it is essentially bins for numeric values.
What I want to do is to convert these to numeric values so I can use them in the downstream analysis. The idea is simple enough; (a) get a function that takes the factor level, split it at the dash and extract numeric values and calculates the average and (b) apply the function of the column
data$Range.mean <- sapply(data$Range, function(d) { range <- as.matrix(strsplit(as.character(d), "-")) (as.numeric(range[,1]) + as.numeric(range[,2]))/2 }) Which gives the following error
Error in FUN(X[[1L]], ...) : (list) object cannot be coerced to type 'double' I tried lapply instead which makes no difference. While looking for answers, I found some other solutions to this problem, which is essentially extracting the lower and upper bound separately to individual arrays then of course calculating pairwise average is trivial.
I would like to understand what I am doing/thinking wrong here though. Why is my code giving an error, and what does that error mean, really?
strsplit(as.character(d), "-")[[1]]?unlist(strsplit(as.character(d), "-"))?unlistseems to do the trick, together with changing the way I access the first and second element. Although I am still a bit confused as to why I got the error message in the first place. The error message is a bit cryptic to me.?strsplityou'll see that the value of this function is alist.as.numerictries to be applied on a "list-y" object and gives this error.