不确定您是否可以将标签作为单独的向量,但这是一个想法。假设你的文件名是x.txt
## set up an argument list for scan() - just to avoid repetition
scanArgs <- list(
file = "x.txt", what = "", nlines = 1, sep = ",", strip.white = TRUE
)
## read the data with no header and add the first line as names
df <- setNames(
read.table("x.txt", skip = 2, sep = ","),
do.call(scan, scanArgs)
)
# BFI1 BFI2 CAQ1 CAQ2
# 1 3 7 1 4
# 2 4 5 3 3
## make the label vector
labels <- setNames(do.call(scan, c(scanArgs, skip = 1)), names(df))
# BFI1 BFI2 CAQ1 CAQ2
# "Likes to read" "Enjoys Parties" "Is Nervous" "Loves Books"
所以中的元素labels
对应于中的列,df
并且列是数字的。
请注意,它x.txt
是用
txt <- 'BFI1, BFI2, CAQ1, CAQ2
Likes to read, Enjoys Parties, Is Nervous, Loves Books
3,7,1,4
4,5,3,3'
writeLines(txt, "x.txt")