내 텍스트 파일은 다음과 같습니다.
"
file1
cols=
col1
col2
# this is a comment
col3
data
a,b,c
d,e,f
"
보시다시피 데이터는 data
태그 뒤에서 만 시작 되고 그 이전의 행은 기본적으로 열 이름이 무엇인지 알려줍니다. data
태그 앞의 행 수가 변수 임을 의미하는 주석이있을 수 있습니다 .
R에서 어떻게 파싱 할 수 있습니까? 아마도 일부와 tidy
도구? 예상 출력은 다음과 같습니다.
# A tibble: 2 x 3
col1 col2 col3
<chr> <chr> <chr>
1 a b c
2 d e f
감사!
다음은 scan()
. strip.white = T
빈 줄 comment.char = "#"
을 제거하고 #
.
text <- scan("test.txt", "", sep = "\n", strip.white = T, comment.char = "#")
text
# [1] "file1" "cols=" "col1" "col2" "col3" "data" "a,b,c" "d,e,f"
ind1 <- which(text == "cols=")
ind2 <- which(text == "data")
df <- read.table(text = paste(text[-seq(ind2)], collapse = "\n"),
sep = ",", col.names = text[(ind1 + 1):(ind2 - 1)])
df
# col1 col2 col3
# 1 a b c
# 2 d e f
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다