Reputation: 25
I have a list with 10 dataframes with each 6 columns and 5553 rows.
Two of the columns are numeric. I want to compare those two columns row by row and pick the highest number and put in a 7th column or as a vector. I have tried different ifelse options and also if statements, but nothing seems to work.
Upvotes: 0
Views: 74
Reputation: 7200
here is my solution, maybe a little clumsy but it might be useful to give you an idea.
# Create some data for reproducibility
da1 <- rnorm(10, mean = 5)
da2 <- rnorm(10, mean = 5)
data <- data.frame(da1, da2)
# Create a loop
maxs <- data.frame()
for(i in 1:nrow(data)) {
maxs <- rbind(maxs, max(data[i ,1 ], data[i, 2] ))
}
Upvotes: 0
Reputation: 887891
Try
indx <- sapply(df, is.numeric)
df$newcol <- do.call(pmax, df[indx])
If there are NAs, you can use na.rm=TRUE
df$newcol <- do.call(pmax, c(df[indx], na.rm=TRUE))
df <- structure(list(V1 = c(8L, 7L, 2L, 3L, 8L, 1L, 10L, 3L, 5L, 2L,
6L, 1L, 3L, 7L, 9L, 4L, 9L, 10L, 8L, 7L), V2 = c("m", "j", "h",
"f", "b", "b", "k", "j", "g", "i", "m", "w", "m", "s", "o", "z",
"l", "h", "e", "d"), V3 = c(6L, 2L, 6L, 3L, 4L, 4L, 3L, 9L, 4L,
9L, 4L, 8L, 10L, 2L, 5L, 6L, 7L, 1L, 5L, 5L), V4 = c("m", "g",
"h", "d", "s", "i", "y", "z", "t", "m", "d", "f", "a", "z", "q",
"i", "o", "v", "a", "s"), V5 = c("h", "f", "s", "n", "r", "x",
"h", "t", "u", "g", "p", "j", "r", "r", "i", "x", "f", "b", "n",
"d"), V6 = c("c", "o", "s", "d", "f", "r", "b", "p", "q", "b",
"i", "g", "j", "d", "y", "f", "s", "q", "s", "z")), .Names = c("V1",
"V2", "V3", "V4", "V5", "V6"), row.names = c(NA, -20L),
class = "data.frame")
Upvotes: 4