library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(readxl)
## Warning: package 'readxl' was built under R version 3.4.4
fruits<-read_xlsx("E:/r/fruits.xlsx")
head(fruits)
## # A tibble: 6 x 3
## fruits weight cost
## <chr> <dbl> <dbl>
## 1 mangoes 9.00 27.0
## 2 apples 18.0 54.0
## 3 oranges 4.00 8.00
## 4 bananas 12.0 76.0
## 5 pineapples 14.0 50.0
## 6 watermalons 26.0 18.0
# column wise percentage with dplyr
fruit_pct<-mutate(fruits,weight_pct=weight/sum(weight),
cost_pct=cost/sum(cost))
head(fruit_pct)
## # A tibble: 6 x 5
## fruits weight cost weight_pct cost_pct
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 mangoes 9.00 27.0 0.0891 0.114
## 2 apples 18.0 54.0 0.178 0.228
## 3 oranges 4.00 8.00 0.0396 0.0338
## 4 bananas 12.0 76.0 0.119 0.321
## 5 pineapples 14.0 50.0 0.139 0.211
## 6 watermalons 26.0 18.0 0.257 0.0761
# by for() while() lapply() sapply
sapply(names(fruits)[-1],function(x){
fruits[paste0(x,"pct")]<- fruits[x]/sum(fruits[x])
})
## $weight.weight
## [1] 0.08910891 0.17821782 0.03960396 0.11881188 0.13861386 0.25742574
## [7] 0.17821782
##
## $cost.cost
## [1] 0.11416490 0.22832981 0.03382664 0.32135307 0.21141649 0.07610994
## [7] 0.01479915
for (col in names(fruits)[-1]) {
fruits[paste0(col,"pct")] <- fruits[col]/sum(fruits[col])
}