簡體   English   中英

從 r 中的多個文本文件(可能有不平衡的行列數)中讀取前 4 列

[英]reading first 4 columns from a multiple text files (which might have unbalance number of columns by rows) in r

我有以下類似文本 (csv) 文件要閱讀,其中前四列是我感興趣的,但之后我有很多垃圾。 我只想將前四列讀入 R。

在此處輸入圖片說明

我想要前四列,以便輸出(在 excel 中打開的 csv)看起來像:

在此處輸入圖片說明

由於 SO 的限制,我無法粘貼整個文件或附加它。 這是鍛煉的較小示例:

type,latitude,longitude,name,link1,
W,43.075319,-89.386145,Mirch Masala,"<just link, jjksskkls  hskks > ","<just link, jjksskkls  hskks > "
W,43.07488,-89.390698,Himal Chuli Restaurant,"<just link, jjksskkls  hskks > ","<just link,  hskks , hsksks  > "
W,43.074887,-89.391011,Chautara Restaurant,"<just link,  hskks , hsksks  > ","<just link, jjksskkls  hskks > "
W,43.092866,-89.351587,Dobhan Restaurant,"<just link, jjksskkls  hskks > ","<just link, jjksskkls , ssjjs hskks > "
W,43.074746,-89.393137,State Street Cash Mart,"<just link, jjksskkls  hskks > ","<just link, jjksskkls , ssjjs hskks > "
W,43.072801,-89.395718,Dotty Dumplings Dowry,"<just link, jjksskkls ,    hskks > ","<just link, jjksskkls , ssjjs hskks > "
W,43.074744,-89.393046,Dobra Tea,"<just link, jjksskkls  hskks > ","<just link, jjksskkls , ssjjs hskks > "
W,43.076372,-89.380231,Hi-Madison,"<just link, jjksskkls  hskks > ","<just link, jjksskkls , ssjjs hskks > "
W,43.019624,-89.421822,Candlewood Suites Fitchburg,"<just link, jjksskkls , ssjjs hskks > ","<just link, jjksskkls , ssjjs hskks > "
W,43.08154,-89.524094,Holiday Inn Hotel & Suites Madison West,"<just link, jjksskkls 100  hskks > ","<just link, jjksskkls , ssjjs hskks > "

在導入到 R 時只讀取前四列有什么想法嗎?

根據您對問題的評論,您的標題有些誤導。 您遇到問題的地方是不知道最終data.frame將具有的確切列數。

?read.table幫助頁面:

count.fields 可用於確定導致錯誤記錄長度報告的讀取文件問題

所以,讓我們嘗試一個不同的答案。

首先,讓這代表您的數據:

"W",43.075319,-89.386145,"Mirch Masala","<J, K>"
"W",43.07488,-89.390698,"Himal Chuli Restaurant","<J, K>","<J, K>","<J, K>"
"W",43.074887,-89.391011,"Chautara Restaurant","<J, K>","<J, K>"
"W",43.092866,-89.351587,"Dobhan Restaurant","<J, K>","<J, K>","<J, K>","<J, K>"
"W",43.074746,-89.393137,"State Street Cash Mart","<J, K>"
"W",43.072801,-89.395718,"Dotty Dumplings Dowry"

(如果這已經保存為文本或 csv 文件,則您不需要下一步,但為了最小的可重現示例......)

將這些行寫入文本文件以模擬read.table過程:

writeLines('"W",43.075319,-89.386145,"Mirch Masala","<J, K>"
"W",43.07488,-89.390698,"Himal Chuli Restaurant","<J, K>","<J, K>","<J, K>"
"W",43.074887,-89.391011,"Chautara Restaurant","<J, K>","<J, K>"
"W",43.092866,-89.351587,"Dobhan Restaurant","<J, K>","<J, K>","<J, K>","<J, K>"
"W",43.074746,-89.393137,"State Street Cash Mart","<J, K>"
"W",43.072801,-89.395718,"Dotty Dumplings Dowry"', "myRaggedFile.txt")

這將創建一個“ read.csv ”的文件,以使用read.tableread.csv 不過,訣竅是使用count.fields來確定文件應該有多少列。

dat <- read.csv("myRaggedFile.txt", header=FALSE, 
                col.names=1:max(count.fields("myRaggedFile.txt", sep=",")))
dat
#      X1       X2        X3                     X4     X5     X6     X7     X8
# 1     W 43.07532 -89.38614           Mirch Masala <J, K>                     
# 2     W 43.07488 -89.39070 Himal Chuli Restaurant <J, K> <J, K> <J, K>       
# 3     W 43.07489 -89.39101    Chautara Restaurant <J, K> <J, K>              
# 4     W 43.09287 -89.35159      Dobhan Restaurant <J, K> <J, K> <J, K> <J, K>
# 5     W 43.07475 -89.39314 State Street Cash Mart <J, K>                     
# 6     W 43.07280 -89.39572  Dotty Dumplings Dowry            
dat <- dat[1:4] # To keep just the first four columns
## Or, continuing with my original answer:
## read.csv("myRaggedFile.txt", header=FALSE, 
##          col.names=1:max(count.fields("myRaggedFile.txt", sep=",")))[1:4]

當您讀入文件時,請使用以下內容:

fist4columns <- read.table("/file/path/filename.csv", header=TRUE, sep=",")[, c(1:4)]

暫無
暫無

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

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