Wie wäre es im folgenden Beispiel möglich, den Schritt des Schreibens in die Datei "test.txt" zu überspringen, d. H. Das cat-Ergebnis einem Objekt zuzuordnen und trotzdem das gleiche Endergebnis zu erzielen?Wie wird die Ausgabe von cat einem Objekt zugewiesen?
Ich dachte, ich würde das vollständige Beispiel einschließen, um Hintergrund zu meinem Problem zu geben.
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
Ergebnisse in:
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