Rename data and calculate %

I need to rename data from a column of a data.frame. What I need the code to do for me is to change the name of one column regarding 4 conditions. To put you in context, I have a data.frame ("trayectorias") with around 200000 rows and 40 columns. One of those columns is "wtc", that is the one I would like to change. That column contains info about aircraft type. Now, it contains an M, L, H or L/M. What I want the code to do is to change that letter to numbers:

  • "L" to a 0
  • "M" to a 1
  • "H" to a 2
  • "L/M" to a 01
    Then, I would also like to count the % of the aircraft (data.frame "trayectorias") type 0, type 1, type 2 and type 01.

Thanks in advance for your help.

I change the characters in the column wt to the characters "0", "1", "2", "01".

library(dplyr)

set.seed(1)
DF <- data.frame(wt = sample(c("L", "M", "H", "L/M"), 15, replace = TRUE))
DF
#>     wt
#> 1    M
#> 2    M
#> 3    H
#> 4  L/M
#> 5    L
#> 6  L/M
#> 7  L/M
#> 8    H
#> 9    H
#> 10   L
#> 11   L
#> 12   L
#> 13   H
#> 14   M
#> 15 L/M
DF <- DF %>% mutate(wt = case_when(wt == "L" ~ "0", wt == "M" ~ "1", wt == "H" ~ "2", wt == "L/M" ~ "01"))
DF
#>    wt
#> 1   1
#> 2   1
#> 3   2
#> 4  01
#> 5   0
#> 6  01
#> 7  01
#> 8   2
#> 9   2
#> 10  0
#> 11  0
#> 12  0
#> 13  2
#> 14  1
#> 15 01
Perc <- DF %>% group_by(wt) %>% summarize(Percent = n()/nrow(DF) * 100)                    
Perc
#> # A tibble: 4 x 2
#>   wt    Percent
#>   <chr>   <dbl>
#> 1 0        26.7
#> 2 01       26.7
#> 3 1        20  
#> 4 2        26.7

Created on 2019-12-14 by the reprex package (v0.2.1)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.