简体   繁体   中英

How to read data where there variables with different lengths in R?

I have a data set stored in a txt file. The variable x and y have different lengths. Therefore I could not manage to read them in R. Here is how my data looks like

x     y    z
1     2    3.2
1.5  1.6   2.5
3    0.8   3.1
2.5        2.4
2.6        2.2

I tried this, but did not work as I desired. Any idea why?

data <- read.table(data.txt,header=T,fill=T)

Here is how it looks

 x     y    z
1     2    3.2
1.5  1.6   2.5
3    0.8   3.1
2.5  2.4
2.6  2.2

Questions that say "did not work" are often too vague to answer. I tried your problem and got a result rather than an error, so you now need to clarify in what manner it is not "working" for your needs:

txt <- "x     y
1     2
1.5  1.6
3    0.8
2.5
2.6"
data <- read.table(text=txt,header=T,fill=T)
data
#------
    x   y
1 1.0 2.0
2 1.5 1.6
3 3.0 0.8
4 2.5  NA
5 2.6  NA

For the edited question:
If that is from an Excel file then you should use "\\t" as the sep= argument. If it is a fixed-width file format than you should use read.fwf as the read function.

You could try the readr package with function read_table

data <- read_table("data.txt")

> data
    x   y   z
1 1.0 2.0 3.2
2 1.5 1.6 2.5
3 3.0 0.8 3.1
4 2.5  NA 2.4
5 2.6  NA 2.2

> dim(data)
[1] 5 3

You may use the parameter fill=TRUE and then you could replace NA by zero

a <-read.table(header = TRUE, fill= TRUE, file= "./data/data.txt")
a[is.na(a)] <- 0

the result:

    x   y
1 1.0 2.0
2 1.5 1.6
3 3.0 0.8
4 2.5 0.0
5 2.6 0.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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