【问题标题】:how can i make dataframe from text with spaces in r?如何从 r 中带有空格的文本制作数据框?
【发布时间】:2018-09-07 02:01:04
【问题描述】:

我有以下文字,我想制作一个数据框 下面的文字 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())
"

我想要下面的数据框输出(数字需要四舍五入)

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

有人可以帮我吗?先感谢您 !

【问题讨论】:

  • 如果文件是制表符分隔的.txt,您可以使用b &lt;- read.delim2("tmp.txt", stringsAsFactors = F) 导入,然后使用@Onyambu 的答案

标签: r dataframe text rounding


【解决方案1】:

你可以这样做:

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>

【讨论】:

  • 谢谢!你能做更多关于 round 的事情吗?我需要 aaa 列号由 2 四舍五入。
  • @jerryhan 当然。而不是上面的lapply,你可以做lapply(parse(text=b[,2]),function(x){m=unlist(eval(x));if(!length(m))NA else round(m,2)})
【解决方案2】:

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                                         

【讨论】:

    猜你喜欢
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    相关资源
    最近更新 更多