使用write.table创建带有制表符分隔文件的字段,其中包含逗号

时间:2019-02-26 11:41:58

标签: r

我正在尝试使用write.table将包含已安装软件包的数据帧写入制表符分隔的文件:

ip <- as.data.frame(installed.packages())
rownames(ip) <- NULL
write.table(ip,file="installed_packages.tsv",quote = F,sep = "\t",row.names = F)

不幸的是,这似乎导致了一个文件,其中字段中的逗号有时会导致创建新行。但是,这并不一致。引用字段没有区别。有人有什么想法吗? 编辑:使用quote = T代替了一个坏的输出示例:

"Package"       "LibPath"       "Version"       "Priority"      "Depends"       "Imports"       "LinkingTo"     "Suggests"      "Enhances"      "License"       "License_is_FOSS"       "License_restricts_use" "OS_type"       "MD5sum"        "NeedsCompilation"      "Built"
"abind" "/home/administrator/R/x86_64-pc-linux-gnu-library/3.4" "1.4-5" NA      "R (>= 1.5.0)"  "methods, utils"        NA      NA      NA      "LGPL (>= 2)"   NA      NA      NA      NA      "no"    "3.4.4"
"acepack"       "/home/administrator/R/x86_64-pc-linux-gnu-library/3.4" "1.4.1" NA      NA      NA      NA      "testthat"      NA      "MIT + file LICENSE"    NA      NA      NA      NA      "yes"   "3.4.4"
"addTextLabels" "/home/administrator/R/x86_64-pc-linux-gnu-library/3.4" "0.0.0.9000"    NA      "R (>= 3.4.4)"  NA      NA      NA      NA      "GPL-3" NA      NA      NA      NA      NA      "3.4.4"
"backports"     "/home/administrator/R/x86_64-pc-linux-gnu-library/3.4" "1.1.2" NA      "R (>= 3.0.0)"  "utils" NA      NA      NA      "GPL-2" NA      NA      NA      NA      "yes"   "3.4.4"
"ballgownMod"   "/home/administrator/R/x86_64-pc-linux-gnu-library/3.4" "2.5.3" NA      "R (>= 3.1.1),
methods"        "GenomicRanges (>= 1.17.25),
IRanges (>= 1.99.22),
S4Vectors (>= 0.9.39),
RColorBrewer,
splines,
sva,
limma,
rtracklayer (>= 1.29.25),
Biobase (>= 2.25.0),
GenomeInfoDb"   NA      "testthat,
knitr"  NA      "Artistic-2.0"  NA      NA      NA      NA      NA      "3.4.4"
"base64enc"     "/home/administrator/R/x86_64-pc-linux-gnu-library/3.4" "0.1-3" NA      "R (>= 2.9.0)"  NA      NA      NA      "png"   "GPL-2 | GPL-3" NA      NA      NA $
"bdsmatrix"     "/home/administrator/R/x86_64-pc-linux-gnu-library/3.4" "1.3-3" NA      "methods, R (>= 2.0.0)" NA      NA      NA      NA      "LGPL-2"        NA      NA $
"broom" "/home/administrator/R/x86_64-pc-linux-gnu-library/3.4" "0.5.0" NA      "R (>= 3.1)"    "backports, dplyr, methods, nlme, purrr, reshape2, stringr,
tibble, tidyr"  NA      "AER, akima, AUC, bbmle, betareg, biglm, binGroup, boot, brms,
btergm, car, caret, coda, covr, e1071, emmeans, ergm, gam (>=
1.15), gamlss, gamlss.data, gamlss.dist, geepack, ggplot2,
glmnet, gmm, Hmisc, irlba, joineRML, Kendall, knitr, ks,
Lahman, lavaan, lfe, lme4, lmodel2, lmtest, lsmeans, maps,
maptools, MASS, Matrix, mclust, mgcv, muhaz, multcomp, network,
nnet, orcutt (>= 2.2), ordinal, plm, plyr, poLCA, psych,
quantreg, rgeos, rmarkdown, robust, rsample, rstan, rstanarm,
sp, speedglm, statnet.common, survey, survival, testthat,
tseries, xergm, zoo"    NA      "MIT + file LICENSE"    NA      NA      NA      NA      "no"    "3.4.4"

我注意到还为以'gam(> =')结尾的行创建了新行。我还观察到文件在libre-office calc中正确打开了带有正确分隔字段的行(但仅当使用quote = T时)选项),但不使用nano,vi或默认的ubuntu文本编辑器(使用quote = T或quote = F)。因此,这与文本编辑器有关。...

2 个答案:

答案 0 :(得分:0)

您可以使用:

直接写入数据帧
 write.csv(ip,"installed_packages.csv")

此处,字段中的逗号不会导致创建新行。

答案 1 :(得分:0)

我认为这应该可以解决问题:

a <- installed.packages()
a <- a[, 1]
names(a) <- c() # optional line
write.table(a, "installed.tsv", quote = F, sep = "\t", row.names = F)

installed.packages()的结果是一个混乱的矩阵,因此您需要正确地对其进行子集化,以获取所有软件包的向量。

R中的结果:

[1] "abind"               "Amelia"              "ape"              "assertthat"         
[5] "backports"           "base"                "base64enc"        "BBmisc"             
[9] "BelgiumMaps.StatBel" "BH"                  "bindr"            "bindrcpp"    

TextEdit(Mac)中的结果:

abind
Amelia
ape
assertthat
backports
base
base64enc

希望有帮助!