以下の例では、ファイル "test.txt"に書き込む、つまりcat-resultをオブジェクトに割り当て、同じ最終結果を達成するステップをスキップできますか?catの出力をオブジェクトに割り当てる方法は?
問題の背景を説明するための完全な例を含めると思いました。中
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