r - Calculate Percentage Column for List of Dataframes When Total Value is Hidden Within the Rows -
library(tidyverse)
i feel there simple solution i'm stuck. code below creates simple list of 2 dataframes (they same simplicity of example, real data has different values)
loc<-c("montreal","toronto","vancouver","quebec","ottawa","hamilton","total") count<-c("2344","2322","122","45","4544","44","9421") data<-data_frame(loc,count) data2<-data_frame(loc,count) data3<-list(data,data2)
each dataframe has "total" within "loc" column corresponding overall total of "count" column. calculate percentages each dataframe dividing each value in "count" column total, last number in "count" column.
i percentages added new columns each dataframe.
for example, total last number in column, in reality, may mixed anywhere in column , can found corresponding "total" value in "loc" column.
i use purrr , tidyverse:
below example of code, i'm stuck on percentage...
data3%>%map(~mutate(.x,paste0(round(100* (missing percentage),2),"%"))
this solution uses base-r:
for (i in seq_along(data3)) { data3[[i]]$count <- as.numeric(data3[[i]]$count) n <- nrow(data3[[i]]) data3[[i]]$perc <- data3[[i]]$count / data3[[i]]$count[n] } > data3 [[1]] # tibble: 7 x 3 loc count perc <chr> <dbl> <dbl> 1 montreal 2344 0.248805859 2 toronto 2322 0.246470651 3 vancouver 122 0.012949793 4 quebec 45 0.004776563 5 ottawa 4544 0.482326717 6 hamilton 44 0.004670417 7 total 9421 1.000000000 [[2]] # tibble: 7 x 3 loc count perc <chr> <dbl> <dbl> 1 montreal 2344 0.248805859 2 toronto 2322 0.246470651 3 vancouver 122 0.012949793 4 quebec 45 0.004776563 5 ottawa 4544 0.482326717 6 hamilton 44 0.004670417 7 total 9421 1.000000000
Comments
Post a Comment