将不同长度的数据文件导入R并熔化它们
Importing data files of different lengths into R and melting them
我有 35 个名为 PXX physiology.txt 的文件,其中 XX 是 1 到 35。例如
head(P1)
Time SkinTemp HeartRate RespirationRate
1 0 27.412 70 10
2 0 25.608 70 10
3 4 25.609 70 10
4 5 25.619 70 15
5 8 25.629 76 14
6 9 25.659 78 14
要导入一个文件,我通常会这样做:
P1 <- read.table("P1 physiology.txt", header = FALSE, skip=14, nrow =
length(readLines("P1 physiology.txt")) - 16)
colnames(P1)<- c("Time","SkinTemp","HeartRate","RespirationRate")
我想将所有 35 个导入到 R 中的某个对象中,使其处于融化格式。 IE。来自所有 35 个文件的所有数据一个接一个地排列,每个数据块都有一个标签。我想融化它的原因是,我可以使用 ggplot2 或 base 根据标签绘制它。
编辑:到目前为止的代码:
我从 中找到了这段代码,并试图更改它但没有成功:
z <- list.files(pattern = ".*\.txt$")
z <- lapply(1:length(z), function(x) {chars <- strsplit(z[x], "");
cbind(data.frame(read.table(z[x])), participant = chars[[1]][1]})
z <- do.call(rbind, z)
# 1. this returns all path location of your desired files
# replace .csv with .txt or whichever ext is yours
l = list.files(path = "path_where_all files_present", pattern = ".csv", full.names = T)
# now iterate over each path, read the data , you could use(read.table instead of csv) and then add the 'id' column
# seq_along() returns indices of l
# you can add `setNames()` after reading.
library(dplyr)
l2 = bind_rows(lapply(l, read.csv), .id = "id")
我有 35 个名为 PXX physiology.txt 的文件,其中 XX 是 1 到 35。例如
head(P1)
Time SkinTemp HeartRate RespirationRate
1 0 27.412 70 10
2 0 25.608 70 10
3 4 25.609 70 10
4 5 25.619 70 15
5 8 25.629 76 14
6 9 25.659 78 14
要导入一个文件,我通常会这样做:
P1 <- read.table("P1 physiology.txt", header = FALSE, skip=14, nrow =
length(readLines("P1 physiology.txt")) - 16)
colnames(P1)<- c("Time","SkinTemp","HeartRate","RespirationRate")
我想将所有 35 个导入到 R 中的某个对象中,使其处于融化格式。 IE。来自所有 35 个文件的所有数据一个接一个地排列,每个数据块都有一个标签。我想融化它的原因是,我可以使用 ggplot2 或 base 根据标签绘制它。
编辑:到目前为止的代码:
我从
z <- list.files(pattern = ".*\.txt$")
z <- lapply(1:length(z), function(x) {chars <- strsplit(z[x], "");
cbind(data.frame(read.table(z[x])), participant = chars[[1]][1]})
z <- do.call(rbind, z)
# 1. this returns all path location of your desired files
# replace .csv with .txt or whichever ext is yours
l = list.files(path = "path_where_all files_present", pattern = ".csv", full.names = T)
# now iterate over each path, read the data , you could use(read.table instead of csv) and then add the 'id' column
# seq_along() returns indices of l
# you can add `setNames()` after reading.
library(dplyr)
l2 = bind_rows(lapply(l, read.csv), .id = "id")