如何使用dplyr::mutate对tible列中的数字进行四舍五入
我有以下几点:如何使用dplyr::mutate对tible列中的数字进行四舍五入,r,dplyr,R,Dplyr,我有以下几点: library(tidyverse) tb <- structure(list(V2 = structure(c(2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L ), .Label = c("bar", "foo", "gop", "qux"), class = "factor"), V3m = c(9.394981, 6.826405, 1.074885, 1.493691, 1e-04, 2e-04, 3e-04, 4e-04)), c
library(tidyverse)
tb <- structure(list(V2 = structure(c(2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L
), .Label = c("bar", "foo", "gop", "qux"), class = "factor"),
V3m = c(9.394981, 6.826405, 1.074885, 1.493691, 1e-04, 2e-04,
3e-04, 4e-04)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-8L), .Names = c("V2", "V3m"))
我们可以使用sprintf
使用@Eric Fail编辑的可复制示例
titanic_4
# A tibble: 6 x 1
# perc_survived
# <dbl>
#1 50.00000
#2 100.00000
#3 97.14286
#4 44.44444
#5 100.00000
#6 19.23077
titanic_4 <- titanic_4 %>%
mutate(perc_survived_6 = sprintf("%0.6f", perc_survived))
titanic_4
# A tibble: 6 x 2
# perc_survived perc_survived_6
# <dbl> <chr>
#1 50.00000 50.000000
#2 100.00000 100.000000
#3 97.14286 97.142857
#4 44.44444 44.444444
#5 100.00000 100.000000
#6 19.23077 19.230769
数据
我们可以使用sprintf
使用@Eric Fail编辑的可复制示例
titanic_4
# A tibble: 6 x 1
# perc_survived
# <dbl>
#1 50.00000
#2 100.00000
#3 97.14286
#4 44.44444
#5 100.00000
#6 19.23077
titanic_4 <- titanic_4 %>%
mutate(perc_survived_6 = sprintf("%0.6f", perc_survived))
titanic_4
# A tibble: 6 x 2
# perc_survived perc_survived_6
# <dbl> <chr>
#1 50.00000 50.000000
#2 100.00000 100.000000
#3 97.14286 97.142857
#4 44.44444 44.444444
#5 100.00000 100.000000
#6 19.23077 19.230769
数据
我们是要将其舍入还是只显示X位数?@zx8754 9.394981到9.39和0.000100到0.00tb%>%mutateV3mR=roundV3m,2@AdamQuek我认为他们不需要舍入.tb%>%mutateV3mR=formatCV3m,format=f,digits=2?我们是要舍入还是只显示X个数字?@zx8754 9.394981到9.39和0.000100到0.00tb%>%mutateV3mR=roundV3m,2@AdamQuek我认为他们不需要round.tb%>%mutateV3mR=formatCV3m,format=f,digits=2?
tb %>%
mutate(V3mr = sprintf("%0.2f", V3m))
titanic_4
# A tibble: 6 x 1
# perc_survived
# <dbl>
#1 50.00000
#2 100.00000
#3 97.14286
#4 44.44444
#5 100.00000
#6 19.23077
titanic_4 <- titanic_4 %>%
mutate(perc_survived_6 = sprintf("%0.6f", perc_survived))
titanic_4
# A tibble: 6 x 2
# perc_survived perc_survived_6
# <dbl> <chr>
#1 50.00000 50.000000
#2 100.00000 100.000000
#3 97.14286 97.142857
#4 44.44444 44.444444
#5 100.00000 100.000000
#6 19.23077 19.230769
titanic_4 <- tibble(perc_survived = c(50.000000,
100.000000, 97.142857, 44.444444, 100.000000, 19.230769))