如何将数据写入R中的文件?

时间:2018-04-13 10:52:42

标签: r

大家好

如果我使用print时数据(kegg.gs)如下所示,但我想将其写入文件。我试过了:

write.csv(kegg.gs, "mygs.csv")

不起作用。

我试过cat,write.table,

fwrite(x = kegg.gs, file = "keggGS.txt", sep = ",", col.names=T)

write.csv2(kegg.gs,"kegg.txt", row.names=FALSE) or TRUE not working too. 

我通常得到类似的东西:

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 65, 30, 27, 32, 36, 26, 6, 43, 9, 19, 16, 7, 57, 132, 162, 99, 44, 11, 3, 54, 29, 41, 17, 42, 5, 22, 10, 4, 1, 50, 49, 46, 48, 15, 25, 80, 59, 20, 40, 14, 18, 24, 2, 64, 23, 13,  

我使用错误的参数还是没有使用正确的功能?

Kegg.gs:
========
> $`hsa04973 Carbohydrate digestion and absorption`  [1] "10000"  "207" 
> "208"    "23439"  "23533"  "2538"   "2542"   "276"     [9] "277"   
> "278"    "279"    "280"    "3098"   "3099"   "3101"   "346562" [17]
> "3938"   "476"    "477"    "478"    "480"    "481"    "482"    "483"  
> [25] "486"    "5290"   "5291"   "5293"   "5294"   "5295"   "5296"  
> "5330"   [33] "5579"   "57818"  "6476"   "6514"   "6518"   "6523"  
> "776"    "80201"  [41] "80834"  "83756"  "8503"   "8972"  
> 
> $`hsa04974 Protein digestion and absorption`  [1] "10008"  "10136" 
> "11136"  "117247" "1277"   "1278"   "1280"   "1281"    [9] "1282"  
> "1284"   "1286"   "1287"   "1288"   "1289"   "1290"   "1291"   [17]
> "1292"   "1293"   "1297"   "1298"   "1299"   "1301"   "1302"   "1303" 
> [25] "1306"   "1308"   "131873" "1357"   "1358"   "1359"   "1360"  
> "1361"   [33] "1504"   "1506"   "1803"   "2006"   "206358" "23428" 
> "23436"  "23439"  [41] "340024" "3769"   "3783"   "3784"   "4224"  
> "4225"   "4311"   "476"    [49] "477"    "478"    "480"    "481"   
> "482"    "483"    "486"    "50509"  [57] "51032"  "5222"   "54407" 
> "5547"   "5644"   "5645"   "5646"   "59272"  [65] "63036"  "643834"
> "643847" "6505"   "6510"   "6519"   "6520"   "6543"   [73] "6546"  
> "6547"   "6550"   "6564"   "7373"   "7512"   "80781"  "8645"   [81]
> "9056"

1 个答案:

答案 0 :(得分:0)

您的规格定义不正确,但这可能是合适的。

#some example data
test <- list(a = 1:5, b = 10:20)

setwd("E:/temp") #any directory
sink(file = "test.txt") #divert R output to file
print(test)
sink(NULL)