I want to find the maximum value in each row across three columns and have the new column print the name of the highest value column.
An example table would appear as follows:
x = c(1,2,5,4,5 )
y = c(2,3,3,1,1 )
z = c(4,4,2,1,1 )
df<-data.frame(x,y,z)
I want to create this:
id | x | y | z | max |
---|---|---|---|---|
1 | 1 | 2 | 4 | z |
2 | 2 | 3 | 4 | z |
3 | 5 | 3 | 2 | x |
4 | 4 | 1 | 1 | x |
5 | 5 | 1 | 1 | x |
I tried:
df%>% rowwise() %>% mutate(max = max(x, y, z))
And received the output:
id | x | y | z | max |
---|---|---|---|---|
1 | 1 | 2 | 4 | 4 |
2 | 2 | 3 | 4 | 4 |
3 | 5 | 3 | 2 | 5 |
4 | 4 | 1 | 1 | 4 |
5 | 5 | 1 | 1 | 5 |
Does anybody know how I can correct this code to yield the desired outcome?