我們可以使用固定寬度閱讀:
基本功能read.fwf
:
x1 <- read.fwf("temp.txt",
widths = c(2, 17, 17, 17),
col.names = paste0("myColName",1:4),
stringsAsFactors = FALSE)
# check output
str(x1)
# 'data.frame': 3 obs. of 4 variables:
# $ myColName1: chr "0M" "0F" "0M"
# $ myColName2: chr "HOCAN000006026421" "HOUSA000002272874" "HOUSA000002272874"
# $ myColName3: chr "HOCAN000000392457" "HOUSA000002272874" "HOUSA000002076121"
# $ myColName4: chr "HOCAN000005311227" "HOUSA000050206641" "HOUSA000014569699"
x1
# myColName1 myColName2 myColName3 myColName4
# 1 0M HOCAN000006026421 HOCAN000000392457 HOCAN000005311227
# 2 0F HOUSA000002272874 HOUSA000002272874 HOUSA000050206641
# 3 0M HOUSA000002272874 HOUSA000002076121 HOUSA000014569699
使用read_fwf
從readr
包:
library(readr)
x2 <- read_fwf("temp.txt",
fwf_widths(c(2, 17, 17, 17),
col_names = paste0("myColName",1:4)))
# check output
str(x2)
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 4 variables:
# $ myColName1: chr "0M" "0F" "0M"
# $ myColName2: chr "HOCAN000006026421" "HOUSA000002272874" "HOUSA000002272874"
# $ myColName3: chr "HOCAN000000392457" "HOUSA000002272874" "HOUSA000002076121"
# $ myColName4: chr "HOCAN000005311227" "HOUSA000050206641" "HOUSA000014569699"
# - attr(*, "spec")=List of 2
# ..$ cols :List of 4
# .. ..$ myColName1: list()
# .. .. ..- attr(*, "class")= chr "collector_character" "collector"
# .. ..$ myColName2: list()
# .. .. ..- attr(*, "class")= chr "collector_character" "collector"
# .. ..$ myColName3: list()
# .. .. ..- attr(*, "class")= chr "collector_character" "collector"
# .. ..$ myColName4: list()
# .. .. ..- attr(*, "class")= chr "collector_character" "collector"
# ..$ default: list()
# .. ..- attr(*, "class")= chr "collector_guess" "collector"
# ..- attr(*, "class")= chr "col_spec"
x2
# # A tibble: 3 × 4
# myColName1 myColName2 myColName3 myColName4
# <chr> <chr> <chr> <chr>
# 1 0M HOCAN000006026421 HOCAN000000392457 HOCAN000005311227
# 2 0F HOUSA000002272874 HOUSA000002272874 HOUSA000050206641
# 3 0M HOUSA000002272874 HOUSA000002076121 HOUSA000014569699
這些解決方案應該工作,即使ID不以字母H
開始,ID可以包含多個H
。
看看被接受的答案,你應該提到你正在從文件中讀取數據。 –