2009-09-10 87 views
17

在下面的例子中,如何跳過寫入文件「test.txt」的步驟,即將cat-result賦值給一個對象,並且仍然達到相同的最終結果?如何將貓的輸出分配給對象?

我想我會包括完整的例子給我的問題的背景。

test <- c("V 1", "x", "1 2 3", "y", "3 5 8", "V 2", "x", "y", "V 3", "y", "7 2 1", "V 4", "x", "9 3 7", "y") 

# Write selection to file 
cat(test, "\n", file="test.txt") 
test2 <- readLines("test.txt") 
test3 <- strsplit(test2, "V ")[[1]][-1] 

# Find results 
x <- gsub("([0-9]) (?:x)?([0-9] [0-9] [0-9])?.*", "\\1 \\2 ", test3, perl = TRUE) 
y <- gsub("([0-9]).* y ?([0-9] [0-9] [0-9])?.*", "\\1 \\2 ", test3, perl = TRUE) 

# Eliminate tests with no results 
x1 <- x[regexpr("[0-9] ([^0-9]).*", x) == -1] 
y1 <- y[regexpr("[0-9] ([^0-9]).*", y) == -1] 

# Dataframe of results 
xdf1 <- read.table(textConnection(x1), col.names=c("id","x1","x2","x3")) 
ydf1 <- read.table(textConnection(y1), col.names=c("id","y1","y2","y3")) 
closeAllConnections() 

# Dataframe of tests with no results 
x2 <- x[regexpr("[0-9] ([^0-9]).*", x) == 1] 
y2 <- y[regexpr("[0-9] ([^0-9]).*", y) == 1] 

df1 <- as.integer(x2[x2 == y2]) 
df1 <- data.frame(id = df1) 

# Merge dataframes 
results <- merge(xdf1, ydf1, all = TRUE) 
results <- merge(results, df1, all = TRUE) 
results 

結果:

id x1 x2 x3 y1 y2 y3 
1 1 1 2 3 3 5 8 
2 2 NA NA NA NA NA NA 
3 3 NA NA NA 7 2 1 
4 4 9 3 7 NA NA NA 

回答

13

而不是cat荷蘭國際集團到一個文件,爲什麼不使用paste命令生成的字符串呢?

> paste(test, collapse="\n") 
[1] "V 1\nx\n1 2 3\ny\n3 5 8\nV 2\nx\ny\nV 3\ny\n7 2 1\nV 4\nx\n9 3 7\ny" 

現在不是做一個cat然後readlines你可以直接通過這個字符串轉換爲strsplit

2

嘗試

> f <- textConnection("test3", "w") 
> cat(test, "\n", file=f) 
> test3 
[1] "V 1 x 1 2 3 y 3 5 8 V 2 x y V 3 y 7 2 1 V 4 x 9 3 7 y " 
> close(f) 
27

作爲更一般的解決方案,您可以使用捕捉輸出功能。它產生一個字符向量,其元素對應於輸出的每一行。

你的例子:

test2<-capture.output(cat(test)) 

這裏是一個多行的例子:

> out<-capture.output(summary(lm(hwy~cyl*drv,data=mpg))) 
> out 
[1] ""                
[2] "Call:"               
[3] "lm(formula = hwy ~ cyl * drv, data = mpg)"      
[4] ""                
[5] "Residuals:"              
[6] " Min  1Q Median  3Q  Max "      
[7] "-8.3315 -1.4139 -0.1382 1.6479 13.5861 "      
[8] ""                
[9] "Coefficients:"             
[10] "   Estimate Std. Error t value Pr(>|t|) "   
[11] "(Intercept) 32.1776  1.2410 25.930 < 2e-16 ***"   
[12] "cyl   -2.0049  0.1859 -10.788 < 2e-16 ***"   
[13] "drvf   8.4009  1.8965 4.430 1.47e-05 ***"   
[14] "drvr   8.2509  6.4243 1.284 0.200 "   
[15] "cyl:drvf  -0.5362  0.3422 -1.567 0.119 "   
[16] "cyl:drvr  -0.5248  0.8379 -0.626 0.532 "   
[17] "---"                
[18] "Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 " 
[19] ""                
[20] "Residual standard error: 2.995 on 228 degrees of freedom"  
[21] "Multiple R-squared: 0.7524,\tAdjusted R-squared: 0.747 "   
[22] "F-statistic: 138.6 on 5 and 228 DF, p-value: < 2.2e-16 "  
[23] ""  
1

另外還有一個賦值語句,它允許你建立一個名稱和對象設置它。如果你想迭代一堆測試並用動態值命名它們非常有用。

分配( 「MARY」,膏(測試,九月= 「\ n」))

將漿料聲明分配給瑪麗。然而,假設您正在運行一系列迴歸,並希望您的迴歸對象由預測變量命名。你可以這樣做

assign(paste("myRegression",names(dataframe)[2],sep=""), lm(dataframe$response~dataframe[,2])) 

這將使你的對象

myRegressionPredictorName你線性模型。

1

試試下面的代碼:

writeLines(capture.out((summary(lm(hwy~cyl*drv,data=mpg)),con="summary.txt",sep="\n")

然後你就可以打開txt文件 「的summary.txt」 查看結果。