2013-03-13 44 views
1

我有三個文本文件。我想要做一些計算,如下所示並繪製結果。 所有文本文件包含14列X1到X14和601行。 該代碼基本上從所有三個文件中讀取X3並進行一些計算,然後返回結果。如何循環R中的文本文件?

ref= read.table("D:\\ref.txt", sep="",header=TRUE)# read first file 
    sour1 = read.table("D:\\sour1.txt", sep="",header=TRUE)# read second file 
    sour2= read.table("D:\\sour2.txt", sep="",header=TRUE,na.rm=TRUE)# read third file 
    result1 = (mean(ref$X3) - ((sd(ref$X3)/sd(sour1$X3))*mean(sour1$X3))+ ((sd(ref$X3)/sd(sour1$X3)*sour1$X3))) # calculate using ref and sour1 
result2 = ((mean(ref$X3) - ((sd(ref$X3)/sd(sour2$X3,na.rm=TRUE))*mean(sour2$X3,na.rm=TRUE))+((sd(ref$X3)/sd(sour2$X3,na.rm=TRUE)*sour2$X3)))) # calculate using ref and sour2 
plot(ref$X3,result1,ylab="Weight in pounds",xlab="Weight in pounds",col=2) 
points(ref$X3,ref$X3, col = 'green') 
points(ref$X3,result2, col = 'blue') # from this I get one plot showing 3 variables on y axis against one on x axis. 

這是使用X3從所有數據僅供情節,但我仍然有其他列X1到X14 我的問題是,我該怎麼做同樣的事情,與所有其他列,最後會得到14個地塊。

+2

你應該給你的數據不是所有文件的一些樣本,並簡化問題,包括只對感興趣的部分(應用功能他人列),否則這個問題太本地化。 – agstudy 2013-03-13 11:47:17

+0

確保你的代碼依賴於2列('X3'和'X1')。你的意思是? (循環2列意味着不同的bahviour循環超過1列。) – 2013-03-13 12:01:34

+0

對不起,我輸入錯誤。我的代碼依賴於1列:X3 – 2013-03-13 12:50:26

回答

2

正如Pop提到的那樣,您需要創建一個列名稱列表並循環這些列名稱。

lapplyfor循環提供了一個稍微優雅的替代方案。

通過更清晰地佈置代碼,可以看到在分配result1result2的行中有一些奇怪的雙括號。爲了清晰起見,考慮將這些行分成更小的計算。

columns <- paste0("X", 1:14) 
lapply(
    columns, 
    function(column) 
    { 
    result1 <- (
     mean(ref[[column]]) - 
     ((sd(ref[[column]])/sd(sour1[[column]])) * mean(sour1[[column]])) + 
     ((sd(ref[[column]])/sd(sour1[[column]]) * sour1[[column]])) 
    ) # calculate using ref and sour1 
    result2 <- (( 
     mean(ref[[column]]) - 
     ((sd(ref[[column]])/sd(sour2[[column]], na.rm=TRUE)) * mean(sour2[[column]], na.rm=TRUE)) + 
     ((sd(ref[[column]])/sd(sour2[[column]], na.rm=TRUE) * sour2[[column]])) 
    )) # calculate using ref and sour2 
    plot(
     ref[[column]], 
     result1, 
     ylab = "Weight in pounds", 
     xlab = "Weight in pounds", 
     col = 2 
    ) 
    points(ref[[column]], ref[[column]], col = 'green') 
    points(ref[[column]], result2, col = 'blue') 
    } 
) 
+0

感謝您的幫助。但我得到這個錯誤:'在plot.window錯誤(.. ):需要有限的'ylim'值 另外:警告消息: 1:在min(x)中:沒有非缺少參數min;返回Inf 2:在max(x)中:沒有非缺失參數爲max;返回-Inf' – 2013-03-13 12:46:00

+1

這可能意味着在你的繪圖中有'NA'或'Inf',因此你的微積分有錯誤,或者這些'NA'值已經在你的文件中。 – Pop 2013-03-13 12:54:33

+0

哦,是的,我的數據中有NAs。但我對待他們'na.rm = TRUE'.應該不是問題! – 2013-03-13 12:57:26

2

要獲得Xii從1到14,你必須使用paste函數獲得元素在列表中的另一種方法:的ref[["X3"]]代替ref$X3

它給你的例子:

for (i in 1:14){ 
     name <- paste('X',i,sep='') 
     result1 = (mean(ref[[name]]) - ((sd(ref[[name]])/sd(sour1[[name]]))*mean(sour1[[name]]))+ ((sd(ref[[name]])/sd(sour1[[name]])*sour1[[name]]))) # calculate using ref and sour1 
     result2 = ((mean(ref[[name]]) - ((sd(ref[[name]])/sd(sour2[[name]],na.rm=TRUE))*mean(sour2[[name]],na.rm=TRUE))+((sd(ref[[name]])/sd(sour2[[name]],na.rm=TRUE)*sour2[[name]])))) # calculate using ref and sour2 
     plot(ref[[name]],result1,ylab="Weight in pounds",xlab="Weight in pounds",col=2) 
     points(ref[[name]],ref$X1, col = 'green') 
     points(ref[[name]],result2, col = 'blue') 
}