如何使用r中的空格从文本中创建数据框?

问题描述 投票:1回答:2

我有下面的文字,我想在文本下面做一个数据框aaa列有列表,数字和数字之间有空格..当我尝试fread(a)它输出不是我想要的..

a<-"
   number    aaa
    1        list(list(10.4444, 11.3333, 12.3333))
    2        list(list(10.3333, 11.3333, 12.3333, 13.3333, 14.3333, 15.3333))
    3        list(list(20.3333))
    4        list(list())
"

我想要下面的数据帧输出(数字需要舍入2)

number        aaa 
    1        10.44,11.33,12.33
    2        10.33,11.33,12.33,13.33,14.33,15.33
    3        20.33
    4        NA

有人能帮助我吗?先感谢您 !

r dataframe text rounding
2个回答
2
投票

你可以做:

b = read.table(text=gsub('\\b(\\w+) ','\\1:',a),h=T,sep=":",strip.white = T,stringsAsFactors = F)
b$aaa = lapply(parse(text=b[,2]),function(x)unlist(eval(x)))

b
  number                                                  aaa
1      1                            10.4444, 11.3333, 12.3333
2      2 10.3333, 11.3333, 12.3333, 13.3333, 14.3333, 15.3333
3      3                                              20.3333
4      4                                                 NULL

请注意,上面的aaa是数据框中的列表,值是数字:

另一方面,你可以这样做:

read.table(text = gsub('(?m)(?<=:).*\\(|\\).*','',gsub('\\b(\\w+) ','\\1:',a),perl=T),sep = ":",na.strings = "",h=T,stringsAsFactors = F,strip.white = T)

  number                                                  aaa
1      1                            10.4444, 11.3333, 12.3333
2      2 10.3333, 11.3333, 12.3333, 13.3333, 14.3333, 15.3333
3      3                                              20.3333
4      4                                                 <NA>

0
投票

data.table变种

library(data.table)
setDT(df)[,.(number,gsub("\\)\\)","",gsub("list\\(","",aaa)))]

   number                                              V2
1:      1                         10.4444,11.3333,12.3333
2:      2 10.3333,11.3333,12.3333,13.3333,14.3333,15.3333
3:      3                                         20.3333
4:      4                                                

要获得四舍五入的数字,您可以尝试

temp1<-strsplit(gsub("\\)\\)","", gsub("list\\(", "", df$aaa)), split = ",") # removing characters list( and )) and split the result
temp2 <- lapply(temp1, function(x) round(as.numeric(x),2)) # converting to numeric and rounding

data.frame(number= df$number,
      new= unlist((lapply(temp2,paste,collapse = ", "))))

  number                                      new
1      1                      10.44, 11.33, 12.33
2      2 10.33, 11.33, 12.33, 13.33, 14.33, 15.33
3      3                                    20.33
4      4                                         
© www.soinside.com 2019 - 2024. All rights reserved.