How can I read in R, numerous txt files with different number of columns? e.g I have the following data:
6 4 2
5 5 3
3 3 4 5 6
4 5 6 7 8
6 3 2 5 8 4 3
1 4 5 0 5 4 8
Thanks
How can I read in R, numerous txt files with different number of columns? e.g I have the following data:
6 4 2
5 5 3
3 3 4 5 6
4 5 6 7 8
6 3 2 5 8 4 3
1 4 5 0 5 4 8
Thanks
How about this?
df <- read.table(file="test.txt", fill=T)
Output is:
V1 V2 V3 V4 V5 V6 V7
1 6 4 2 NA NA NA NA
2 5 5 3 NA NA NA NA
3 3 3 4 5 6 NA NA
4 4 5 6 7 8 NA NA
5 6 3 2 5 8 4 3
6 1 4 5 0 5 4 8
Sample data:
6 4 2
5 5 3
3 3 4 5 6
4 5 6 7 8
6 3 2 5 8 4 3
1 4 5 0 5 4 8
in test.txt
In base R you can do something like this using readLines
and strsplit
:
# Store sample data in file sample.txt
cat(
"6 4 2
5 5 3
3 3 4 5 6
4 5 6 7 8
6 3 2 5 8 4 3
1 4 5 0 5 4 8
", file = "sample.txt")
# Read data line by line
data <- readLines("sample.txt");
# Split entries by line, returns list
lst <- lapply(data, function(x) unlist(strsplit(x, " ")));
#[[1]]
#[1] "6" "4" "2"
#
#[[2]]
#[1] "5" "5" "3"
#
#[[3]]
#[1] "3" "3" "4" "5" "6"
#
#[[4]]
#[1] "4" "5" "6" "7" "8"
#
#[[5]]
#[1] "6" "3" "2" "5" "8" "4" "3"
#
#[[6]]
#[1] "1" "4" "5" "0" "5" "4" "8"
Now written as a custom function, though I am sure Prems solution is sufficient.
library(data.table)
data <- readLines("sample.txt")
read_different <- function(data) {
list_of_rowdata <- lapply(data, function(x) data.frame(matrix(unlist(strsplit(x, " ")), nrow = 1)))
my_table <- data.frame(rbindlist(list_of_rowdata, fill = T))
return(my_table)
}
read_different(data)