[英]R: Issues reading in tab delimited files
為這個簡單的問題提前道歉。 我在閱讀制表符分隔文件時遇到問題。 R認為第164行有缺失的元素,但我不明白為什么。 當我復制並粘貼到Excel時,它分離就好了。
數據:
temp <- tempfile()
download.file("https://www.fda.gov/downloads/Drugs/InformationOnDrugs/UCM527389.zip",temp)
我努力了
df <- read.table(unz(temp, "Products.txt"), sep="\t",header= TRUE)
和
df <- read.table(unz(temp, "Products.txt"), sep="\t",fill=TRUE, header= TRUE)
這在同一條線上搞砸了。
考慮read.delim
,其中read.csv
是內置utils
包中更通用的read.table
函數的包裝器。
看起來較長的字段DrugName和ActiveIngredient存在引號和空行問題,需要調整fill , quote , comment_char參數。
df <- read.delim(unz(temp, "Products.txt"), sep="\t", header= TRUE)
結構輸出:
str(df)
# 'data.frame': 37850 obs. of 8 variables:
# $ ApplNo : int 4 159 552 552 552 552 552 552 552 552 ...
# $ ProductNo : num 4 1 1 2 3 4 5 7 8 9 ...
# $ Form : Factor w/ 348 levels "AEROSOL, FOAM;RECTAL",..: 203 331 121 121 121 121 121 121 121 121 ...
# $ Strength : Factor w/ 4065 levels ""," EQ 5MG BASE/ML",..: 525 2491 1453 2240 2447 538 654 670 538 2447 ...
# $ ReferenceDrug : int 0 0 0 0 0 0 0 0 0 0 ...
# $ DrugName : Factor w/ 7161 levels "8-HOUR BAYER",..: 4773 6039 3547 3547 3547 3547 3547 3546 2796 2796 ...
# $ ActiveIngredient : Factor w/ 2735 levels "ABACAVIR SULFATE",..: 1372 2446 1305 1305 1305 1305 1305 1305 1305 1305 ...
# $ ReferenceStandard: int 0 0 0 0 0 0 0 0 0 0 ...
與read.table
等效,調整參數中的默認值:
df <- read.table(unz(temp, "Products.txt"), sep="\t", quote = "\"", fill = TRUE,
comment.char = "", header= TRUE)
為了比較:
df1 <- read.table(unz(temp, "Products.txt"), sep="\t", quote = "\"", fill = TRUE,
comment.char = "", header= TRUE)
df2 <- read.delim(unz(temp, "Products.txt"), sep="\t", header= TRUE)
all.equal(df1, df2)
# [1] TRUE
identical(df1, df2)
# [1] TRUE
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.