-3

I have a data frame with three columns and four rows. I want to create a new column based on available columns such that new column gets the maximum value of the corresponding row (no matter if there is NA or not). If all are NAs, the new column gets NA. enter image description here

Thanks.

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
Sh B
  • 21
  • 4

1 Answers1

1

We could use pmax with na.rm specified as TRUE (assuming it is a data.frame object and the missing values are NA)

df1$new_column <- do.call(pmax, c(df1, na.rm = TRUE))

-output

> df1
   A   B   C new_column
1 98  NA  NA         98
2 NA  NA  NA         NA
3 98 100  NA        100
4 98 100 200        200

data

df1 <- data.frame(A = c(98, NA, 98, 98), B = c(NA, NA, 100, 100),
    C = c(NA, NA, NA, 200))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    Thank you for providing the data. – TarJae Dec 25 '21 at 19:19
  • Thank you for your helpful reply. If I want to create the new column based on available columns in data frame which shows the majority and if there is no majority takes 3, how would it be possible? like this: df1 <- data.frame(A = c(2, NA, 2,1), B = c(1, NA, 2,NA), C = c(2,1, NA, 2) then new column=c(2,1,2,3) – Sh B Dec 25 '21 at 21:54
  • @ShB For that we may need to use [Mode](https://stackoverflow.com/questions/31400445/r-how-to-find-the-mode-of-a-vector) and then use `apply(df1, 1, function(x) Mode(na.omit(x)))`. If you want the tied cases to 3, have to modify the function – akrun Dec 25 '21 at 21:57