使用不同的分隔符将数据读入 R

read data into R with different delimiters

我正在尝试将一个文件读入 R,该文件在第一行中具有不同的分隔符 space 作为分隔符,但从第二行到第一列和第二列之间的最后一行有一个 space,第二个和第三个相同,那么所有的block of two,zeros和ones应该是不同的列。 有什么提示吗?!

ID Chip AX-77047182 AX-80910836 AX-80737273 AX-77048714 AX-77048779 AX-77050447 
3811582 1 2002202222200202022020200200220200222200022220002200000201202000222022
3712982 1 2002202222200202022020200200220200222200022220002200000200202000222022
3712990 1 2002202211200202021011100101210200111101022121112100111110211110122122
3713019 1 2002202211200202021011100101210200111101022121112100111110211110122122
3713025 1 2002202211200202021011100101210200111101022121112100111110211110122122
3713126 1 2002202222200202022020200200220200222200022220002200000200202000222022

你可以试试... read(pattern = " || 1 ", recursive = TRUE) 绑定后

例如:

data <- "ID Chip AX-77047182 AX-80910836 AX-80737273 AX-77048714 AX-77048779 AX-77050447 
3811582 1 2002202222200202022020200200220200222200022220002200000201202000222022
3712982 1 2002202222200202022020200200220200222200022220002200000200202000222022
3712990 1 2002202211200202021011100101210200111101022121112100111110211110122122
3713019 1 2002202211200202021011100101210200111101022121112100111110211110122122
3713025 1 2002202211200202021011100101210200111101022121112100111110211110122122
3713126 1 2002202222200202022020200200220200222200022220002200000200202000222022"

teste <- strsplit(data, split = "\n")

for(i in seq(1, length(teste[[1]]),1)) {
  if (i==1) {
    dataOut <- strsplit(teste[[1]][i], split = " ")
    print(dataOut)
  } else
    dataOut <- strsplit(teste[[1]][i], split = " 1 ")
  print(dataOut)
}

当然不是最优雅的解决方案,但您可以尝试以下方法。如果我正确理解了您的示例数据,那么您没有提供 zeros/ones/twos 的行所需的所有列名(AX-77047182,...)。如果我的理解有误,下面的方法不会产生预期的结果,但可能仍会帮助您找到解决方法 - 您可以简单地调整第二个拆分命令中的分隔符。我希望这有助于...

#read file as character vector
chipstable <- readLines(".../chips.txt")

#extact first line to be used as column names
tablehead <- unlist(strsplit(chipstable[1], " "))

#split by first delimiter, i.e., space
chipstable <- strsplit(chipstable[2:length(chipstable)], " ")

#split by second delimiter, i.e., between each character (here number) 
#and merge the two split results in one line
chipstable <- lapply(chipstable, function(x) {

  c(x[1:2],  unlist(strsplit(x[3], "")))

})

#combine all lines to a data frame
chipstable <- do.call(rbind, chipstable)

#assign column names
colnames(chipstable) <- tablehead

#turn values to numeric (if needed)
chipstable <- apply(chipstable, 2, as.numeric)