我有了這個格式的.txt文件:閱讀文本文件中的r和存儲所讀條件的下一行
--------------------------------------------------------------------------------------------------------------
m5a2 A2. Confirm how much time child lives with respondent
--------------------------------------------------------------------------------------------------------------
type: numeric (byte)
label: BM_101F
range: [-9,7] units: 1
unique values: 8 missing .: 0/4898
tabulation: Freq. Numeric Label
1383 -9 -9 Not in wave
4 -2 -2 Don't know
2 -1 -1 Refuse
3272 1 1 all or most of the time
29 2 2 about half of the time
76 3 3 some of the time
80 4 4 none of the time
52 7 7 only on weekends
--------------------------------------------------------------------------------------------------------------
m5a3 A3. Number of months ago child stopped living with you
--------------------------------------------------------------------------------------------------------------
type: numeric (int)
label: NUMERIC, but 44 nonmissing values are not labeled
range: [-9,120] units: 1
unique values: 47 missing .: 0/4898
examples: -9 -9 Not in wave
-6 -6 Skip
-6 -6 Skip
-6 -6 Skip
--------------------------------------------------------------------------------------------------------------
什麼是對我很重要,是代號,如m5a2
時,說明A2. Confirm how much time child lives with respondent
,最後,響應
tabulation: Freq. Numeric Label
1383 -9 -9 Not in wave
4 -2 -2 Don't know
2 -1 -1 Refuse
3272 1 1 all or most of the time
29 2 2 about half of the time
76 3 3 some of the time
80 4 4 none of the time
52 7 7 only on weekends
我需要閱讀這三個項目將進行進一步的處理列表中的值。
我已經嘗試了以下內容,它在檢索代碼名稱和說明時起作用。
fileName <- "../data/ff_mom_cb9.txt"
conn <- file(fileName,open="r")
linn <-readLines(conn)
L = list()
for (i in 1:length(linn)){
if((linn[i]=="--------------------------------------------------------------------------------------------------------------") & (linn[i+1]!=""))
{
L[i] = linn[i+1]
}
else
{
# read until hit the next dashed line
}
}
close(conn)
有幾件事情我感到困惑: 1.我不知道如何讓它讀取行,直到遇到下下虛線。 2.如果我希望能夠可視化搜索並輕鬆檢索數據,我的方法是否正確地將讀取的數據存儲在列表中?
謝謝。
它似乎一次只讀一行到'D'? – Waht
如果你試試這個,該怎麼辦? –
或者如果你有頭文件,只要將它改爲TRUE而不是FALSE –