簡體   English   中英

如何在 R 中讀取具有不同列數的 CSV 文件

[英]How can you read a CSV file in R with different number of columns

我有一個 csv 格式的稀疏數據集,其列數的長度各不相同。 這是文件文本的示例。

12223, University
12227, bridge, Sky
12828, Sunset
13801, Ground
14853, Tranceamerica
14854, San Francisco
15595, shibuya, Shrine
16126, fog, San Francisco
16520, California, ocean, summer, golden gate, beach, San Francisco

當我使用

read.csv("data.txt", header = F)

R 會將數據集解釋為具有 3 列,因為大小是根據前 5 行確定的。 無論如何要強制 r 將數據放在更多列中?

?read.table文檔的深處有以下內容:

數據列的數量是通過查看輸入的前五行(或整個文件,如果它少於五行)來確定的,或者從col.names的長度來確定,如果它被指定並且更長。 如果fillblank.lines.skip are true ,這可能是錯誤的,因此如有必要,請指定col.names (如“示例”中所示)。

因此,讓我們將col.names定義為長度 X(其中 X 是數據集中字段的最大數量),並設置fill = TRUE

dat <- textConnection("12223, University
12227, bridge, Sky
12828, Sunset
13801, Ground
14853, Tranceamerica
14854, San Francisco
15595, shibuya, Shrine
16126, fog, San Francisco
16520, California, ocean, summer, golden gate, beach, San Francisco")

read.table(dat, header = FALSE, sep = ",", 
  col.names = paste0("V",seq_len(7)), fill = TRUE)

     V1             V2             V3      V4           V5     V6             V7
1 12223     University                                                          
2 12227         bridge            Sky                                           
3 12828         Sunset                                                          
4 13801         Ground                                                          
5 14853  Tranceamerica                                                          
6 14854  San Francisco                                                          
7 15595        shibuya         Shrine                                           
8 16126            fog  San Francisco                                           
9 16520     California          ocean  summer  golden gate  beach  San Francisco

如果最大字段數未知,您可以使用漂亮的實用函數count.fields (我在read.table示例代碼中找到):

count.fields(dat, sep = ',')
# [1] 2 3 2 2 2 2 3 3 7
max(count.fields(dat, sep = ','))
# [1] 7

可能有用的相關閱讀: 僅閱讀 R 中有限數量的列

你可以這樣讀取數據:

dat <- textConnection("12223, University
12227, bridge, Sky
12828, Sunset
13801, Ground
14853, Tranceamerica
14854, San Francisco
15595, shibuya, Shrine
16126, fog, San Francisco
16520, California, ocean, summer, golden gate, beach, San Francisco")

dat <- readLines(dat)
dat <- strsplit(dat, ",")

這會產生一個列表。

這似乎確實有效(遵循@BlueMagister 的建議):

tt <- read.table("~/Downloads/tmp.csv", fill=TRUE, header=FALSE, 
          sep=",", colClasses=c("numeric", rep("character", 6)))
names(tt) <- paste("V", 1:7, sep="")

     V1             V2             V3      V4           V5     V6             V7
1 12223     University                                                          
2 12227         bridge            Sky                                           
3 12828         Sunset                                                          
4 13801         Ground                                                          
5 14853  Tranceamerica                                                          
6 14854  San Francisco                                                          
7 15595        shibuya         Shrine                                           
8 16126            fog  San Francisco                                           
9 16520     California          ocean  summer  golden gate  beach  San Francisco

試試這個,它有點更有活力..

readVariableWidthFile <- function(filePath){
  con <-file(filePath)
  lines<- readLines(con)
  close(con)
  slines <- strsplit(lines,",")
  colCount <- max(unlist(lapply(slines, length)))

  FileContent <- read.csv(filePath,
                        header = FALSE,
                        col.names = paste0("V",seq_len(colCount)),
                        fill = TRUE)
  return(FileContent)
}

我遇到了類似的挑戰,但 Blue Magister 的答案中的count.fields不起作用,可能是因為字段內的逗號與sep=","沖突。 此外,列數因文件而異。 所以我只是在read.table定義了多余的col.names (在我的例子中 100 就足夠了),然后我使用which(!is.na())來去除多余的列。

dat <- read.table("path/to/file.csv", col.names = paste("V",1:100), fill = T, sep = ",")
dat <- dat[,which(!is.na(dat[1,]))]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM