Here is my code and output.
aggregate(age, by=list(membership),FUN=mean,na.rm=TRUE)
Group.1 x
1 1 73.50558
2 2 72.44681
3 3 71.07353
4 4 70.55556
5 5 69.30769
6 6 62.81818
7 7 73.53659
I want to see 73.51. How to modify my code above?
Here is my code and output.
aggregate(age, by=list(membership),FUN=mean,na.rm=TRUE)
Group.1 x
1 1 73.50558
2 2 72.44681
3 3 71.07353
4 4 70.55556
5 5 69.30769
6 6 62.81818
7 7 73.53659
I want to see 73.51. How to modify my code above?
You can use round
with the parameter digits
to specify how many decimal places to display.
round(73.50558, digits = 2)
#> [1] 73.51
Created on 2020-09-28 by the reprex package (v0.3.0)
But how to combine round and aggregate ?
I think you might need to rewrite the FUN
bit like this: (example from the aggregate
help file):
library(datasets)
aggregate(state.x77, list(Region = state.region), function(x) round(mean(x, na.rm = TRUE), 2))
#> Region Population Income Illiteracy Life Exp Murder HS Grad Frost
#> 1 Northeast 5495.11 4570.22 1.00 71.26 4.72 53.97 132.78
#> 2 South 4208.12 4011.94 1.74 69.71 10.58 44.34 64.62
#> 3 North Central 4803.00 4611.08 0.70 71.77 5.28 54.52 138.83
#> 4 West 2915.31 4702.62 1.02 71.23 7.22 62.00 102.15
#> Area
#> 1 18141.00
#> 2 54605.12
#> 3 62652.00
#> 4 134463.00
Created on 2020-09-28 by the reprex package (v0.3.0)
Try this for the entire data:
dataframe[is.num] <- lapply(dataframe[is.num], round, 2)
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.