How can I convert my list values into numeric and then sort them in ascending?
scores <- split(data$scores, data$Id) %>%
lapply(., function(x) {
as.numeric(x[2])
sort(x, decreasing = TRUE, na.last = NA)
})
I'm not sure how to access the second part of my list, and so they are remaining as characters.
The list output is as follows and has been generated by split(). The name of each element in the list is the 'ID' and the value of each element are the scores, but these are characters.
Scores list looks something like this:
CodePudding user response:
Assuming your data looks something like this:
my_scores <- list(ID = 1, Scores = c("1", "5", "3"))
You could do:
lapply(my_scores, function(x) sort(as.numeric(x)))