Search code examples
rdplyrlapplydata-manipulationsapply

manipulate the column value based on the row names in R


I am trying to manipulate the column value based on the row names, any help will be appreciated.

data(mtcars)
    mtcars$gear1 <- factor(mtcars$gear, levels = c(3,4,5))
    my.mean <- function(x) if(is.numeric(x)) mean(x) else prop.table(table(x))
    B <- setNames(as.data.frame(unlist(lapply(mtcars, FUN = my.mean))), "mean")
    
    
    class(B)
    row.names(B)
    put.per <- c("gear1.3", "gear1.4", "gear1.5")
    
    sapply(row.names(B),function(x){
       if(x %in% put.per) paste(B$mean, "%", sep = "")
       })

Expected Answer

              mean
mpg      20.090625
cyl       6.187500
disp    230.721875
hp      146.687500
drat      3.596563
wt        3.217250
qsec     17.848750
vs        0.437500
am        0.406250
gear      3.687500
carb      2.812500
gear1.3   0.468750%
gear1.4   0.375000%
gear1.5   0.156250%

many thanks in advance


Solution

  • You'll not need sapply or any other kind of loop here. You can find the row index where put.per values are present and change them with paste0.

    inds <- rownames(B) %in% put.per
    B$mean[inds] <- paste0(B$mean[inds], "%")
    B
    
    #              mean
    #mpg      20.090625
    #cyl         6.1875
    #disp    230.721875
    #hp        146.6875
    #drat     3.5965625
    #wt         3.21725
    #qsec      17.84875
    #vs          0.4375
    #am         0.40625
    #gear        3.6875
    #carb        2.8125
    #gear1.3   0.46875%
    #gear1.4     0.375%
    #gear1.5   0.15625%
    

    However, note that a column can have data of only one type so here all the values would turn to character since we are adding % sign.