简体   繁体   中英

how to keep only numbers from a single column in R

I used following code to reconstruct my original dataframe (for the 1st dataset in a list) from output of lapply function (S.list) which gave me a single number for each of the datalist.

 data1$S <- S.list[1]

S.list was extracted from lapply funciton with which I applied a function to a list of 6 datasets and got a single number for each dataset in a list. Then I wanted to reconstruct the original dataframe for all 6 datasets in the list. this is the example I reconstructed for the 1st dataset of the list and I saved the result in S column. But S column returns into something like this

       S 
c(`Julia` = 0.245133133918501)
c(`Julia` = 0.245133133918501)
c(`Julia` = 0.245133133918501)
c(`Julia` = 0.245133133918501)
...

So how can I delete others except from number (0.245133). I need only the number.

This is one way to do it

data1=read.table(text="
      S
      'c(`Julia` = 0.245133133918501)'
      'c(`Julia` = 0.245133133918501)'
      'c(`Julia` = 0.245133133918501)'
      'c(`Julia` = 0.245133133918501)'
", header=T, stringsAsFactors=F)

data1$S = as.numeric(sub("(.+= )([0-9\\.]+)(.+)", "\\2", data1$S, perl = T))

data1$S
# [1] 0.2451331 0.2451331 0.2451331 0.2451331

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