Let's supponse a have a vector consisting of strings, just like this:

> string
[1] "   26     10.00       28.00      28.00      28.00     28.00      28.00    28.00      26"
[2] "   27     10.00       28.00      28.00      28.00     28.00      28.00    28.00      27"
[3] "   28     10.00       28.00      28.00      28.00     28.00      28.00    28.00      28"
[4] "   29     10.00                  28.00      28.00     28.00      28.00    28.00      29"
[5] "   30     10.00                  28.00      28.00     28.00      28.00    28.00      30"
[6] "   31     10.00                  28.00                28.00               28.00      31"

and I want to read it as a data.frame with 6 rows and 9 columns, like this:

  V1 V2 V3 V4 V5 V6 V7 V8 V9
1 26 10 28 28 28 28 28 28 26
2 27 10 28 28 28 28 28 28 27
3 28 10 28 28 28 28 28 28 28
4 29 10 NA 28 28 28 28 28 29
5 30 10 NA 28 28 28 28 28 30
6 31 10 NA 28 NA 28 NA 28 31

where each empty cell is considered as NA. I've tried use read.table and setting fill=TRUE, but it doesn't work since the result is this:

> read.table(textConnection(string), sep="", fill=TRUE)
     V1 V2 V3 V4 V5 V6 V7 V8 V9
    1 26 10 28 28 28 28 28 28 26
    2 27 10 28 28 28 28 28 28 27
    3 28 10 28 28 28 28 28 28 28
    4 29 10 28 28 28 28 28 29 NA
    5 30 10 28 28 28 28 28 30 NA
    6 31 10 28 28 28 31 NA NA NA

I'm stuck on this. Maybe it is easier than I think, but I can't figure out how to do it :(

Here's the data:

string <- c("   26     10.00       28.00      28.00      28.00     28.00      28.00    28.00      26",
                "   27     10.00       28.00      28.00      28.00     28.00      28.00    28.00      27", 
                "   28     10.00       28.00      28.00      28.00     28.00      28.00    28.00      28", 
                "   29     10.00                  28.00      28.00     28.00      28.00    28.00      29", 
                "   30     10.00                  28.00      28.00     28.00      28.00    28.00      30", 
                "   31     10.00                  28.00                28.00               28.00      31")
有帮助吗?

解决方案

Looks like you could use read.fwf():

ff <- tempfile()
cat(file = ff, "12 34 56", "98    54", sep = "\n")
read.fwf(ff, widths=c(3,3,2))
#   V1 V2 V3
# 1 12 34 56
# 2 98 NA 54
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top