必须有一种更优雅的方式,但我坚持这一点;希望能帮助到你:
#some random data at first
set.seed(1)
WT1 <- data.frame(c1 = replicate(3, paste(sample(LETTERS, 4),
sample(0:9, 4), sep = "", collapse = "")), c2 = runif(3), stringsAsFactors = F)
set.seed(2)
WT2 <- data.frame(c1 = replicate(3, paste(sample(LETTERS, 4),
sample(0:9, 4), sep = "", collapse = "")), c2 = runif(3), stringsAsFactors = F)
set.seed(3)
WT3 <- data.frame(c1 = replicate(3, paste(sample(LETTERS, 4),
sample(0:9, 4), sep = "", collapse = "")), c2 = runif(3), stringsAsFactors = F)
#> WT1 #> WT2 #> WT3
# c1 c2 # c1 c2 # c1 c2
#1 G2J8N7U4 0.26722067 #1 E9R8N1D5 0.3472722 #1 E6U5J0H2 0.2368850
#2 Q6B3E9X8 0.38611409 #2 M7N1Y3F5 0.4887732 #2 P5Z9M6L8 0.7911474
#3 S9Y1J5R0 0.01339033 #3 Z6F3K9B1 0.1492469 #3 C2R0V1G8 0.5997316
#separate columns `c1` from `c2` from all dataframes, in a manipulative form
c1s <- mapply(function(...) c(...), WT1$c1, WT2$c1, WT3$c1, SIMPLIFY = F)
names(c1s) <- NULL
c2s <- mapply(function(...) c(...), WT1$c2, WT2$c2, WT3$c2, SIMPLIFY = F)
#a function to calculate everything wanted...
fun <- function(c1, c2)
{
M1 <- max(c2)
M2 <- min(c2)
M3 <- c1[which.max(c2)]
M4 <- c1[which.min(c2)]
data.frame(M1, M2, M3, M4, stringsAsFactors = F)
}
#...for each row
Results <- t(mapply(fun, c1s, c2s))
#> Results
# M1 M2 M3 M4
#[1,] 0.3472722 0.236885 "E9R8N1D5" "E6U5J0H2"
#[2,] 0.7911474 0.3861141 "P5Z9M6L8" "Q6B3E9X8"
#[3,] 0.5997316 0.01339033 "C2R0V1G8" "S9Y1J5R0"