简体   繁体   中英

R: convert list of numbers from character to numeric

I have a column in my dataframe where in every cell there are one or more numbers. If there are many numbers, they are seperated with a space. Furthermore, R considers them as a character vector. I'd really like to convert them to numeric (and if possible sum them up right away). Eg one of my cells might look like

6 310 21 20 64

I've tried

Reduce(sum,L)

and

as.numeric(L)

but I always get Warning message:

NAs introduced by coercion

Here, L is just a sample object I created to put one of my cells into.

Here are two more options which work correctly on a vector (it seems)

str1 <- c("6 310 21 20 64", "6 310 21 20 64","6 310 21 20 64")
rowSums(read.table(text = str1))
## [1] 421 421 421

Or using data.table::fread

rowSums(data.table::fread(paste(str1, collapse = "\n")))
# [1] 421 421 421        

Or as mentioned in comments by @akrun, you can use Reduce(`+`,...) instead of rowSums(...) in both cases in order to avoid to marix conversion

We can use scan

sum(scan(text=str1, what=numeric(), quiet=TRUE))
#[1] 421

data

str1 <- "6 310 21 20 64"

Following my comment, here is a solution using sapply :

sum(as.numeric(strsplit("6 310 21 20 64",' ')[[1]]))

which for the column of the dataframe will give something like this:

sapply(1:nrow(df),function(x){sum(as.numeric(strsplit(str1,' ')[[x]]))})
# 421 421 421

which could be improved in sapply(strsplit(str1,' '), function(x) sum(type.convert(x))) , thanks to David Arenburg's suggestion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM