简体   繁体   中英

mean calculation of a vector in R

I am trying to figure out how can i calculate the mean of a vector.

Vector is S1temp which prints [1] "18, 20.5, 18, 18.6, 21.5" . I 've read searched for a solution but as.numeric and suppressWarnings does not work.

Is there an other way to do achieve this?

thanks,

as.numeric(unlist((strsplit(S1temp,",")))

An alternative to strsplit here is to just use scan :

S1temp
# [1] "18, 20.5, 18, 18.6, 21.5"

X <- scan(text = S1temp, sep = ",")
# Read 5 items

X
# [1] 18.0 20.5 18.0 18.6 21.5

str(X)
#  num [1:5] 18 20.5 18 18.6 21.5

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