2017-12-27 224 views
0

如何使用fwf將行向量(不是文件)讀入數據幀?來自文字數據(不是文件)的數據幀與R

現在,我可以想到兩種方法,但我真的覺得應該有更好的方法。任何想法是讚賞。

  1. 使用data.frame() + substring()。它可以完成這項工作,但如果數據「不整齊」(就像下面的塊一樣),我不能簡單地推廣它。我從這裏得到答案:Read fixed width text file

  2. 使用write_lines()read_fwf() from readr。我想避免寫一個外部文件。實際上,read_fwf()似乎應該直接在文字數據上進行工作,但我無法使其工作:它始終將線條的字符串/矢量理解爲路徑。類似:

    write_lines(literaldata, "fwf_sample.txt") 
    read_fwf("fwf_sample.txt", fwf_widths(rep(8, 12))) 
    

數據樣本如下下方,導致錯誤的代碼。

literaldata <- "CHEXA  278375  2 419991 419976 418527 418528 434131 434116+   420108 420107 
CHEXA  278376  2 420028 420029 419994 419997 434168 434169+   434134 434137 
CHEXA  278377  2 419961 418516 418517 419956 434101 420119+   420118 434096 
CHEXA  278378  2 419965 418519 418520 419967 434105 420116+   420115 434107 
CHEXA  278379  2 419965 419984 420025 419971 434105 434124+   434165 434111 
CHEXA  278380  2 418521 419972 419967 418520 420114 434112+   434107 420115" 

library(readr) 
lines<-read_lines(literaldata) 
# The code above is just to get a reproducible example similar to the one I get in the data cleaning process 
read_fwf(lines, fwf_widths(rep(8, 12))) 


Error: 'CHEXA  278375  2 419991 419976 418527 418528 434131 
434116+   420108 420107CHEXA  278376 ... 

在此先感謝

+0

會'read_fwf(textConnection(literaldata),fwf_widths(REP(8,12)))'做工作? –

+0

你可以發表代碼,其中'read_fwf()'錯誤的文字數據的路徑名?因爲它在你發佈的數據上工作得很好。 –

回答

0

不知道它到底是什麼你在幹什麼。功能read_fwf()適用於您的數據。

literaldata <- "CHEXA  278375  2 419991 419976 418527 418528 434131 434116+   420108 420107 
CHEXA  278376  2 420028 420029 419994 419997 434168 434169+   434134 434137 
CHEXA  278377  2 419961 418516 418517 419956 434101 420119+   420118 434096 
CHEXA  278378  2 419965 418519 418520 419967 434105 420116+   420115 434107 
CHEXA  278379  2 419965 419984 420025 419971 434105 434124+   434165 434111 
CHEXA  278380  2 418521 419972 419967 418520 420114 434112+   434107 420115" 

library(readr) 
read_fwf(literaldata, fwf_widths(rep(8, 12))) 

# # A tibble: 6 x 12 
#  X1  X2 X3  X4  X5  X6  X7  X8  X9 X10 X11 X12 
# <chr> <int> <int> <int> <int> <int> <int> <int> <int> <chr> <int> <int> 
# 1 CHEXA 278375  2 419991 419976 418527 418528 434131 434116  + 420108 420107 
# 2 CHEXA 278376  2 420028 420029 419994 419997 434168 434169  + 434134 434137 
# 3 CHEXA 278377  2 419961 418516 418517 419956 434101 420119  + 420118 434096 
# 4 CHEXA 278378  2 419965 418519 418520 419967 434105 420116  + 420115 434107 
# 5 CHEXA 278379  2 419965 419984 420025 419971 434105 434124  + 434165 434111 
# 6 CHEXA 278380  2 418521 419972 419967 418520 420114 434112  + 434107 420115 

read_fwf()(高亮礦)的文檔:

文字數據是用於實施例和試驗最有用的。它必須包含至少一個新行被識別爲數據(而不是路徑)。

+0

感謝您的回覆,看起來我可以這樣做:
'paste0(vectorOfLines,collapse =「\ n」)' 它爲我完成了這項工作,但它仍然看起來像readr文檔,我不應該需要'paste0()' 文字數據對於示例和測試非常有用。它必須包含至少一個要識別爲數據(而不是路徑)的新行或者是長度大於1 **的**向量。 – loistf

+0

你還沒有提供完整的可重複的例子,所以我們不知道你在做什麼。請閱讀此:https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –

0

我還不清楚爲什麼我以前的例子不起作用,但增加一個paste0(...,collapse = "\n")來完成這項工作。所以,類似下面的工作對我來說:

literaldata <- "CHEXA  278375  2 419991 419976 418527 418528 434131 434116+   420108 420107 
CHEXA  278376  2 420028 420029 419994 419997 434168 434169+   434134 434137 
CHEXA  278377  2 419961 418516 418517 419956 434101 420119+   420118 434096 
CHEXA  278378  2 419965 418519 418520 419967 434105 420116+   420115 434107 
CHEXA  278379  2 419965 419984 420025 419971 434105 434124+   434165 434111 
CHEXA  278380  2 418521 419972 419967 418520 420114 434112+   434107 420115" 

library(readr) 
lines<-read_lines(literaldata) 
# The code above is just to get a reproducible example similar to the one I get in the data cleaning process 
# The following gives an error 
read_fwf(lines, fwf_widths(rep(8, 12))) 
# The following give the expected result 
read_fwf(paste0(lines,collapse = "\n"), fwf_widths(rep(8, 12))) 

感謝大家的幫助和回答