4
How to transform a list into a vector in R? The list is a set of words and numbers and I need to add the numbers present in the list.
mylist <- list ("2 tomates", "5 tomates", "9 tomates")
If I want the total of tomatoes, how to do?
4
How to transform a list into a vector in R? The list is a set of words and numbers and I need to add the numbers present in the list.
mylist <- list ("2 tomates", "5 tomates", "9 tomates")
If I want the total of tomatoes, how to do?
3
At least two ways -- surely there are others:
# note a diferenca entre as listas
mylist <- list ("2 tomates", "5 tomates", "9 tomates")
first,
myvector <- unlist(mylist)
x = regmatches(myvector, regexpr("[0-9]+", myvector))
y = as.numeric(x)
sum(y)
or else
numeros = function(x) {
regmatches(x, regexpr("[0-9]+", x))[1]
}
x = unlist(lapply(mylist, numeros))
y = as.numeric(x)
sum(y)
Both answers assume that the regular expression "[0-9]+"
is what you want.
In my answer, I assumed that mylist
had 3 components, not one as in the original question.
Browser other questions tagged r list
You are not signed in. Login or sign up in order to post.