我正在尝试在R中创建一个数据表,其中有一个带有变量的标题和一个带有每个变量分组的4个“ reps”的子标题。
这是View(my.data)
从csv文件读取的my.data <- read.csv("DataTable.csv", header=T, skip=1)
的结果:
我该怎么做?目前看来不正确...
答案 0 :(得分:2)
由于CSV文件包含两行标题,因此您必须分别加载数据和标题。此外,您应考虑什么是分隔符。在您的情况下,它是空格,而不是逗号。请参见下面的模拟和建议的CSV文件读取过程。
# csv - simulation
set.seed(123)
h1 <- c("", "", "WiId 24h", "", "", "", "WiId 56h", "", "", "")
h2 <- c("Match" ,"ID" ,"Rep1" ,"Rep2" ,"Rep3" ,"Rep4" ,"Rep1" ,"Rep2","Rep3" ,"Rep4")
h1_w <- paste0(h1, sep = " ", collapse = "")
h2_w <- paste0(h2, sep = " ", collapse = "")
df <- data.frame(1:10, 0:9, matrix(rnorm(10 * 8), ncol = 8))
df2 <- rbind(h2, df)
names(df2) <- h1
write.table(df2, file = "test2.csv", sep = " ", row.names = FALSE)
# reading of 2-line csv
header <- read.table("test2.csv", sep = " ", header = TRUE, nrows = 1)
body <- read.table("test2.csv", sep = " ", header = FALSE, skip = 2)
header
# header is:
# X X.1 WiId.24h X.2 X.3 X.4 WiId.56h X.5 X.6 X.7
# 1 Match ID Rep1 Rep2 Rep3 Rep4 Rep1 Rep2 Rep3 Rep4
head(body)
# body is:
# V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
# 1 1 0 -0.56047565 1.2240818 -1.0678237 0.4264642 -0.6947070 0.25331851 0.3796395 -0.4910312
# 2 2 1 -0.23017749 0.3598138 -0.2179749 -0.2950715 -0.2079173 -0.02854676 -0.5023235 -2.3091689
# 3 3 2 1.55870831 0.4007715 -1.0260044 0.8951257 -1.2653964 -0.04287046 -0.3332074 1.0057385
# 4 4 3 0.07050839 0.1106827 -0.7288912 0.8781335 2.1689560 1.36860228 -1.0185754 -0.7092008
# 5 5 4 0.12928774 -0.5558411 -0.6250393 0.8215811 1.2079620 -0.22577099 -1.0717912 -0.6880086
# 6 6 5 1.71506499 1.7869131 -1.6866933 0.6886403 -1.1231086 1.51647060 0.3035286 1.0255714