简体   繁体   中英

calculate percentage in a data frame R

I have a data frame of multiple columns. I want to create a new colomns with the percentage. I don't have the correct output, any suggestions?

il.count.descrition elle.count.description un.count.description   Words.count
     5                      1                      5                    563
     9                      2                      2                    65
     1                      1                      4                    100
     10                     9                      0                    89
data %>% mutate(il.count.ratio = il.count.description / Words.count * 100,
                elle.count.ratio = elle.count.description / Words.count * 100,
                un.count.ratio = un.count.description / Words.count * 100)

You can try this, it works for me. Maybe the error is because you have a typo in the first variable name.

data <- data %>% mutate(il.count.ratio = round(il.count.descrition / Words.count * 100,1),
                elle.count.ratio = round(elle.count.description / Words.count * 100,1),
                un.count.ratio = round(un.count.description / Words.count * 100,1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM