How To Filter And Sum Csv In R
Sum office in R – sum(), is used to calculate the sum of vector elements. sum of a particular cavalcade of a dataframe. sum of a grouping can also calculated using sum() function in R by providing it inside the aggregate function. with sum() function we can besides perform row wise sum using dplyr bundle and likewise column wise sum lets come across an example of each.
- sum of the listing of vector elements with NA values
- Sum of a particular column of the dataframe in R
- column wise sum of the dataframe using sum() office
- Sum of the group in R dataframe using aggregate() and dplyr package
- Row wise sum of the dataframe in R using sum() function
Syntax for sum function :
sum(10, na.rm = FALSE, …)
- 10 – numeric vector
- rm- whether NA should exist removed, if not, NA will exist returned
Instance of sum office in R
sum of vectors is depicted below.
# R sum function sum(i:10) sum(c(ii,5,6,7,ane,2))
output:
Example of sum function with NA:
sum() function doesn't give desired output, If NAs are present in the vector. then information technology has to be handled by using na.rm=True in sum() function
# sum() function in R for input vector which has NA. x = c(1.234,two.342,-4.562,v.671,12.345,-14.567,NA) sum(x,na.rm=TRUE)
output:
[one] 2.463
Example of sum() role in R dataframe:
Lets create the data frame to demonstrate sum role – sum() in r
### create the dataframe my_basket = data.frame(ITEM_GROUP = c("Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable","Vegetable","Dairy","Dairy","Dairy","Dairy","Dairy"), ITEM_NAME = c("Apple","Assistant","Orangish","Mango","Papaya","Carrot","Potato","Brinjal","Raddish","Milk","Curd","Cheese","Milk","Paneer"), Price = c(100,80,80,90,65,70,sixty,lxx,25,lx,forty,35,50,120), Taxation = c(2,iv,5,6,2,iii,v,one,3,4,5,vi,4,three)) my_basket
so the resultant dataframe will be
sum of a column in R data frame using sum() function :
sum() function takes the column proper noun equally argument and calculates the sum of that detail cavalcade
# sum() office in R : sum of a cavalcade in data frame sum(my_basket$Toll)
so the resultant sum of "Price" column will exist
output:
[1] 945
column wise sum using sum() office:
sum() function is applied to the required column through mapply() function, so that it calculates the sum of required column equally shown below.
# sum() part in R : sum of multiple column in information frame mapply(sum,my_basket[,c(-1,-2)])
so the resultant sum of "Price" and "Tax" columns will exist
Sum of the cavalcade by group using sum() function
aggregate() function along with the sum() function calculates the sum of a group. hither sum of "Price" column, for "Item_Group" is calculated.
##### Sum of the column by group aggregate(x= my_basket$Cost, by= list(my_basket$ITEM_GROUP), FUN=sum)
Item_group has three groups "Dairy","Fruit" & "Vegetable". sum of cost for each group is calculated every bit shown below
Sum of the column by grouping and populate it past using sum() role:
group_by() role along with the sum() function calculates the sum of a group. here sum of "Price" column, for "Item_Group" is calculated and populated across as shown below
#### sum of the cavalcade by grouping and populate it using dplyr library(dplyr) my_basket %>% group_by(ITEM_GROUP) %>% mutate(sum_by_group = sum(Toll))
Item_group has three groups "Dairy","Fruit" & "Vegetable". sum of price for each group is calculated and populated as shown below
Row wise sum using sum() function forth with dplyr
Row wise sum is calculated with the assistance rowwise() function of dplyr package and sum() office equally shown beneath
## row wise sum using dplyr library(dplyr) my_basket %>% rowwise() %>% mutate( Total_price = sum(c(Toll,Tax)) )
row wise sum of "Cost" and "Revenue enhancement" is calculated and populated for each row every bit shown beneath
For further understanding of sum() function in R using dplyr one can refer the dplyr documentation
Related Topics:
How To Filter And Sum Csv In R,
Source: https://www.datasciencemadesimple.com/sum-function-in-r/
Posted by: floressirche.blogspot.com
0 Response to "How To Filter And Sum Csv In R"
Post a Comment