繁体   English   中英

将表读入R

[英]Read.table into R

我想将文本文件读取到R中,但是出现一个问题,第一列与列名和第一列编号混合在一起。

资料文字档

revenues       4118000000.0, 4315000000.0, 4512000000.0, 4709000000.0, 4906000000.0, 5103000000.0
cost_of_revenue-1595852945.4985902, -1651829192.2662954, -1705945706.6237037, -1758202488.5708148, -1808599538.1076286, -1857136855.234145
gross_profit   2522147054.5014095, 2663170807.7337046, 2806054293.376296, 2950797511.429185, 3097400461.892371, 3245863144.765855

R代码:data.predicted_values = read.table(“ predicted_values.txt”,sep =“,”)

输出:

                                  V1          V2          V3          V4          V5          V6
1        revenues       4118000000.0  4315000000  4512000000  4709000000  4906000000  5103000000
2 cost_of_revenue-1595852945.4985902 -1651829192 -1705945707 -1758202489 -1808599538 -1857136855
3  gross_profit   2522147054.5014095  2663170808  2806054293  2950797511  3097400462  3245863145

如何将第一列分为两部分? 我的意思是我希望第一列V1是收入,cost_of_revenue,gross_profit。 V2是4118000000.0,-1595852945.4985902,2522147054.5014095。 等等等等。

由于您没有逗号btwn行名和值,因此需要将它们添加回去:

txt <- "revenues       4118000000.0, 4315000000.0, 4512000000.0, 4709000000.0, 4906000000.0, 5103000000.0
cost_of_revenue-1595852945.4985902, -1651829192.2662954, -1705945706.6237037, -1758202488.5708148, -1808599538.1076286, -1857136855.234145
gross_profit   2522147054.5014095, 2663170807.7337046, 2806054293.376296, 2950797511.429185, 3097400461.892371, 3245863144.765855"

Lines <- readLines( textConnection(txt) ) 
  # replace textConnection(.)  with  `file = "predicted_values.txt"`
res <- read.csv( text=sub( "(^[[:alpha:][:punct:]]+)(\\s|-)" ,
                                               "\\1,", Lines) ,
          header=FALSE, row.names=1 )
res

十进制小数可能不会打印,但是它们在那里。

这与@DWin的思路相同,但是在第二行中给出了负值。

TEXT <- readLines("predicted_values.txt")
A <- gregexpr("[A-Za-z_]+", TEXT)
B <- read.table(text = regmatches(TEXT, A, invert = TRUE)[[1]], sep = ",")
C <- cbind(FirstCol = regmatches(TEXT, A)[[1]], B)
C
#          FirstCol          V1          V2          V3          V4          V5          V6
# 1        revenues  4118000000  4315000000  4512000000  4709000000  4906000000  5103000000
# 2 cost_of_revenue -1595852945 -1651829192 -1705945707 -1758202489 -1808599538 -1857136855
# 3    gross_profit  2522147055  2663170808  2806054293  2950797511  3097400462  3245863145

您需要read.tablerow.names参数。 然后,您可以简单地转置数据:

data.predicted_values = read.table("predicted_values.txt", sep=",", row.names=1)
data.predicted_values <- t(data.predicted_values)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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