2013-11-21 41 views
1

我想将一个文本文件读入R,但是我遇到了第一列与列名和第一列数混合的问题。Read.table into 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”,月= “”)

输出:

        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。等等等等。

+0

您的意思是说,你的列是行,反之亦然? –

+0

你是说有一些空间分隔符而不是其他的?例如第2行。 – Maiasaura

回答

1

这与@Dinin's的思路是一样的,但是在第二行占了负值。

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 
1

既然你没有逗号btwn的rownames和你需要的值,将其添加回去:

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 

的小数可能无法打印,但它们的存在。

0

您需要row.names参数read.table。然后你可以简单地转置你的数据:

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

我尝试了两种方法,但没有奏效。我想将行名和数字数据分隔成两个单独的列。 – user3015546

+0

您需要使用'sub'作为我的例子。 –

+0

啊我以为数值是纯粹因为你没有告诉它使用第一列作为'row.names' –