Guest User

Untitled

a guest
Jul 22nd, 2019
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. df <- data.frame(v1 = c(0,1,2,3,4,5,6,7,8,9),
  2. v2 = c(2,1,4,7,6,7,8,9,0,1),
  3. v3 = c(4,1,6,7,8,9,0,1,2,3),
  4. v4 = c(0,1,2,7,4,5,6,7,8,9),
  5. v5 = c(0,1,6,3,6,9,8,9,0,1))
  6.  
  7. df$max <- colnames(df)[apply(df,1,which.max)]
  8.  
  9. v1 v2 v3 v4 v5 v1max v2max v3max v4max v5max
  10. 1 0 2 4 0 0 FALSE FALSE TRUE FALSE FALSE
  11. 2 1 1 1 1 1 TRUE TRUE TRUE TRUE TRUE
  12. 3 2 4 6 2 6 FALSE FALSE TRUE FALSE TRUE
  13. 4 3 7 7 7 3 FALSE TRUE TRUE TRUE FALSE
  14. 5 4 6 8 4 6 FALSE FALSE TRUE FALSE FALSE
  15. 6 5 7 9 5 9 FALSE FALSE TRUE FALSE TRUE
  16. 7 6 8 0 6 8 FALSE TRUE FALSE FALSE TRUE
  17. 8 7 9 1 7 9 FALSE TRUE FALSE FALSE TRUE
  18. 9 8 0 2 8 0 TRUE FALSE FALSE TRUE FALSE
  19. 10 9 1 3 9 1 TRUE FALSE FALSE TRUE FALSE
  20.  
  21. df[paste0(names(df), "max")] <- df == do.call(pmax, df)
  22.  
  23. df
  24. # v1 v2 v3 v4 v5 v1max v2max v3max v4max v5max
  25. #1 0 2 4 0 0 FALSE FALSE TRUE FALSE FALSE
  26. #2 1 1 1 1 1 TRUE TRUE TRUE TRUE TRUE
  27. #3 2 4 6 2 6 FALSE FALSE TRUE FALSE TRUE
  28. #4 3 7 7 7 3 FALSE TRUE TRUE TRUE FALSE
  29. #5 4 6 8 4 6 FALSE FALSE TRUE FALSE FALSE
  30. #6 5 7 9 5 9 FALSE FALSE TRUE FALSE TRUE
  31. #7 6 8 0 6 8 FALSE TRUE FALSE FALSE TRUE
  32. #8 7 9 1 7 9 FALSE TRUE FALSE FALSE TRUE
  33. #9 8 0 2 8 0 TRUE FALSE FALSE TRUE FALSE
  34. #10 9 1 3 9 1 TRUE FALSE FALSE TRUE FALSE
  35.  
  36. df[paste0(names(df), "max")] <- t(apply(df, 1, function(x) x == max(x)))
  37.  
  38. is.max <- function(x, na.rm = TRUE){
  39. x == max(x, na.rm = na.rm)
  40. }
  41.  
  42. res <- t(apply(df, 1, is.max))
  43. colnames(res) <- paste(colnames(res), "max", sep = ".")
  44. res <- cbind(df, res)
  45.  
  46. res
  47. # v1 v2 v3 v4 v5 v1.max v2.max v3.max v4.max v5.max
  48. #1 0 2 4 0 0 FALSE FALSE TRUE FALSE FALSE
  49. #2 1 1 1 1 1 TRUE TRUE TRUE TRUE TRUE
  50. #3 2 4 6 2 6 FALSE FALSE TRUE FALSE TRUE
  51. #4 3 7 7 7 3 FALSE TRUE TRUE TRUE FALSE
  52. #5 4 6 8 4 6 FALSE FALSE TRUE FALSE FALSE
  53. #6 5 7 9 5 9 FALSE FALSE TRUE FALSE TRUE
  54. #7 6 8 0 6 8 FALSE TRUE FALSE FALSE TRUE
  55. #8 7 9 1 7 9 FALSE TRUE FALSE FALSE TRUE
  56. #9 8 0 2 8 0 TRUE FALSE FALSE TRUE FALSE
  57. #10 9 1 3 9 1 TRUE FALSE FALSE TRUE FALSE
  58.  
  59. df %>%
  60. mutate_all(list(max = ~ . == exec(pmax, !!!.)))
  61.  
  62. v1 v2 v3 v4 v5 v1_max v2_max v3_max v4_max v5_max
  63. 1 0 2 4 0 0 FALSE FALSE TRUE FALSE FALSE
  64. 2 1 1 1 1 1 TRUE TRUE TRUE TRUE TRUE
  65. 3 2 4 6 2 6 FALSE FALSE TRUE FALSE TRUE
  66. 4 3 7 7 7 3 FALSE TRUE TRUE TRUE FALSE
  67. 5 4 6 8 4 6 FALSE FALSE TRUE FALSE FALSE
  68. 6 5 7 9 5 9 FALSE FALSE TRUE FALSE TRUE
  69. 7 6 8 0 6 8 FALSE TRUE FALSE FALSE TRUE
  70. 8 7 9 1 7 9 FALSE TRUE FALSE FALSE TRUE
  71. 9 8 0 2 8 0 TRUE FALSE FALSE TRUE FALSE
  72. 10 9 1 3 9 1 TRUE FALSE FALSE TRUE FALSE
  73.  
  74. df2<-cbind(df,df == apply(df,1,max))
  75. colnames(df2)<-c("v1", "v2" ,"v3", "v4" ,"v5", "v1max", "v2max", "v3max" ,"v4max", "v5max")
  76. df2
  77. # v1 v2 v3 v4 v5 v1max v2max v3max v4max v5max
  78. # 1 0 2 4 0 0 FALSE FALSE TRUE FALSE FALSE
  79. # 2 1 1 1 1 1 TRUE TRUE TRUE TRUE TRUE
  80. # 3 2 4 6 2 6 FALSE FALSE TRUE FALSE TRUE
  81. # 4 3 7 7 7 3 FALSE TRUE TRUE TRUE FALSE
  82. # 5 4 6 8 4 6 FALSE FALSE TRUE FALSE FALSE
  83. # 6 5 7 9 5 9 FALSE FALSE TRUE FALSE TRUE
  84. # 7 6 8 0 6 8 FALSE TRUE FALSE FALSE TRUE
  85. # 8 7 9 1 7 9 FALSE TRUE FALSE FALSE TRUE
  86. # 9 8 0 2 8 0 TRUE FALSE FALSE TRUE FALSE
  87. # 10 9 1 3 9 1 TRUE FALSE FALSE TRUE FALSE
RAW Paste Data