0

Say you have a CSV file. Each row of the file has numbers, vectors, and dates. Elements of each vector separated by semi-colons. For example a vector y in this csv file looks like ";1;2;4;7;2". The vectors are different lengths. I couldn't read this file using

read.table() 

or

read.csv()

even with trying some things similar to what was written here How to read a .csv file containing apostrophes into R?. Below is a simplified version of what 3 lines in the CSV file might look like

1,6,;2;3.1;45;31.2;3,2,;1;1;1;1;1;5,10/22/1938 1:25
2,5,;1;22;12;1.4;66,7,;2;3;4;5;6;7;8;6;9,11/25/1938 1:25
3,1,;1;2;3;4;5;6;7;8;9,3.2,;1;2;3;4;5;6;7;9;10;11,11/25/1958 1:25

and here it is with spaces between the commas, to make it a bit more readable

1, 6, ;2;3.1;45;31.2;3, 2, ;1;1;1;1;1;5, 10/22/1938 1:25
2, 5, ;1;22;12;1.4;66, 7, ;2;3;4;5;6;7;8;6;9, 11/25/1938 1:25
3, 1, ;1;2;3;4;5;6;7;8;9, 3.2, ;1;2;3;4;5;6;7;9;10;11, 11/25/1958 1:25

Each line has the same number of ','s, the only major difference between lines is that the vectors can be different. Note sometimes fields may be blank. I think it makes most sense for the output to be in the form of a list of a list. I was thinking of writing my own function that would effectively look something like (I'm not so proficient with lists yet so my language may be way off here)

data <- empty list of a list
while (we haven't reached the end of the file){ #don't know the function to do this
  temp = get first line of file #don't know the function to do this
  if temp is not empty{ #don't know the function to do this
    indices = which(temp==',')
    indices.col = which(temp==';')
    put temp[1:(indices(1)-1)] in the (counter,1) location of data;
    put temp[(indices(1)+1):(indices(2)-1)] in the (counter,2) location of data; 
    store the vector and deal with the colons somehow in (counter,3) location of data;
  }
}

Would there be an easier way to do this, maybe using read.table in a way that I missed. I'm not set on using lists of lists to do this. I want to basically do some regression analysis of the form y=mx+b, where x is one of the numerical entries and y is the scalar output of a function applied to one of the vector entries (eg sum(vector) = a*first entry of row + b). So perhaps keep that in mind. Also note that there would be an option to have this file use some other character besides semi colons to separate the vectors.

4

2 回答 2

2

仍然不知道您在寻找什么,这里有一个建议。

从 G. Grothendieck 的回答中的 DF 开始:

### Optional cleanup to remove the leading semicolon.
### Not doing so will result in a couple of empty columns.
DF$V3 <- gsub("^;", "", DF$V3)
DF$V5 <- gsub("^;", "", DF$V5)

我会concat.split.multiple从我的“splitstackshape”包中建议,因为(1)您可以一次拆分多个列;(2) 每列可以有不同的分隔符;(3) 您可以选择“宽”或“长”的拆分数据显示。melt可以使用“reshape2”包之类的工具进一步操作长表单,dcast为您以后做其他事情提供很大的灵活性。

library(splitstackshape)
concat.split.multiple(DF, c("V3", "V5"), ";")
#   V1 V2  V4              V6 V3_1 V3_2 V3_3 V3_4 V3_5 V3_6 V3_7 V3_8 V3_9 V5_1
# 1  1  6 2.0 10/22/1938 1:25    2  3.1   45 31.2    3   NA   NA   NA   NA    1
# 2  2  5 7.0 11/25/1938 1:25    1 22.0   12  1.4   66   NA   NA   NA   NA    2
# 3  3  1 3.2 11/25/1958 1:25    1  2.0    3  4.0    5    6    7    8    9    1
#   V5_2 V5_3 V5_4 V5_5 V5_6 V5_7 V5_8 V5_9 V5_10
# 1    1    1    1    1    5   NA   NA   NA    NA
# 2    3    4    5    6    7    8    6    9    NA
# 3    2    3    4    5    6    7    9   10    11
out <- concat.split.multiple(DF, c("V3", "V5"), ";", "long")

head(out)
#   V1 V2  V4              V6 time V3 V5
# 1  1  6 2.0 10/22/1938 1:25    1  2  1
# 2  2  5 7.0 11/25/1938 1:25    1  1  2
# 3  3  1 3.2 11/25/1958 1:25    1  1  1
# 4  1  6 2.0 10/22/1938 1:25    2 NA NA
# 5  2  5 7.0 11/25/1938 1:25    2 NA NA
# 6  3  1 3.2 11/25/1958 1:25    2 NA 11
于 2013-11-04T17:00:39.783 回答
1

在使用中阅读它read.csv。然后可以重新读取第 3 列和第 5 列,为每个列创建一个矩阵并用这些矩阵替换它们的列,即第 3 列变成一个矩阵,第 5 列也是如此,如str最后的输出所示:

Lines <- "1,6,;2;3.1;45;31.2;3,2,;1;1;1;1;1;5,10/22/1938 1:25
2,5,;1;22;12;1.4;66,7,;2;3;4;5;6;7;8;6;9,11/25/1938 1:25
3,1,;1;2;3;4;5;6;7;8;9,3.2,;1;2;3;4;5;6;7;9;10;11,11/25/1958 1:25
"

DF <- read.csv(text = Lines, header = FALSE, as.is = TRUE)
DF2 <- transform(DF,
       V3 = as.matrix(read.table(text = V3, sep = ";", fill = TRUE)),
       V5 = as.matrix(read.table(text = V5, sep = ";", fill = TRUE))
    )

str输出。请注意,第 3 列和第 5 列中的每一列本身都是一个矩阵:

> str(DF2)
'data.frame':   3 obs. of  6 variables:
 $ V1: int  1 2 3
 $ V2: int  6 5 1
 $ V3: num [1:3, 1:10] NA NA NA 2 1 1 3.1 22 2 45 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr  "V1" "V2" "V3" "V4" ...
 $ V4: num  2 7 3.2
 $ V5: int [1:3, 1:11] NA NA NA 1 2 1 1 3 2 1 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr  "V1" "V2" "V3" "V4" ...
 $ V6: chr  "10/22/1938 1:25" "11/25/1938 1:25" "11/25/1958 1:25"

另请注意,如果您想将其展平,请尝试:

 DF.flat <- do.call(data.frame, DF2)

补充:如何变平。

于 2013-11-04T05:30:15.200 回答