问题
I have a dataframe which has a few character columns followed by a few numerical columns. I want to add a new column using the %>% operators which is the highest value from the numerical columns per row.
let's say the data frame looks like this:
character1, character2, value1, value2, value3
"string", "string", 5, 7, 4
"string", "string", 3, 4, 2
"string", "string", 2, 8, 6
Then the new column should be 7 for the first row, 4 for second row and 8 for last row. I am trying to use the apply function in the pipe operator but it's not working properly.
new_df <- old_df %>%
mutate(new_column = apply(value1:value3, 1, max))
It returns error: Numerical expression has XXX values only first used.
I also tried using max(value1: value3) instead of apply but that doesn't work either.
回答1:
You can use rowwise
from dplyr
:
library(dplyr)
df %>%
rowwise() %>%
mutate(new_column = max(c_across(value1:value2)))
# A tibble: 3 x 6
# Rowwise:
character1 character2 value1 value2 valu3 new_column
<chr> <chr> <int> <int> <int> <int>
1 string string 5 7 4 7
2 string string 3 4 2 4
3 string string 2 8 6 8
Data
library(tidyverse)
df <- tibble::tribble(
~character1, ~character2, ~value1, ~value2, ~valu3,
"string", "string", 5L, 7L, 4L,
"string", "string", 3L, 4L, 2L,
"string", "string", 2L, 8L, 6L
)
回答2:
We can also use pmax
library(dplyr)
library(purrr)
df %>%
mutate(new_column = reduce(select(., starts_with('value')), pmax))
# A tibble: 3 x 6
# character1 character2 value1 value2 valu3 new_column
# <chr> <chr> <int> <int> <int> <int>
#1 string string 5 7 4 7
#2 string string 3 4 2 4
#3 string string 2 8 6 8
data
df <- structure(list(character1 = c("string", "string", "string"),
character2 = c("string", "string", "string"), value1 = c(5L,
3L, 2L), value2 = c(7L, 4L, 8L), valu3 = c(4L, 2L, 6L)), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame"))
来源:https://stackoverflow.com/questions/62247801/how-to-use-apply-function-in-a-pipe-operator