[英]How can i use paste to insert percentages into a table in R
I am trying to insert a percentage into each cell in a table in R我试图在 R 中的表格中的每个单元格中插入一个百分比
This is the dataframe im working with这是我使用的数据框
1 a 1535 4 0 1539
2 b 768 6 0 774
3 c 112 1 0 113
4 d 279 4 0 283
5 e 231 5 0 236
This is the code i used to produce it:这是我用来生成它的代码:
df <-structure(
list(
category = c(
"a",
"b",
"c",
"d",
"e"
),
group1 = c(1535,
768, 112, 279, 231),
group2 = c(4, 6, 1, 4, 5),
group3 = c(0,
0, 0, 0, 0),
groupall = c(1539, 774, 113, 283, 236)
),
class = c("grouped_df",
"tbl_df", "tbl", "data.frame"),
row.names = c(NA, 5L),
groups = structure(
list(
category = c(
"a",
"b",
"c",
"d",
"e"
),
.rows = list(1L,
2L, 3L, 4L, 5L)
),
row.names = c(NA,-5L),
class = c("tbl_df",
"tbl", "data.frame"),
.drop = FALSE
)
)
I have tried this code:我试过这个代码:
findPerc<- function(x){
percent <- as.numeric(round((x/10000) * 100, digits = 2))
paste(percent, "%", sep = '')
}
then applying it to the dataframe using然后使用它应用到数据帧
df <- apply(df,c(1,2),findPerc)
but this returns the error: Error in x/10000 : non-numeric argument to binary operator但这会返回错误: x/10000 中的错误:二元运算符的非数字参数
my desired output looks like:我想要的输出看起来像:
1 a 1535(15.35%) 4(0.04%) 0(0.00%) 1539(15.39%)
2 b 768 (7.65%) 6(0.06%) 0 (0.00%) 774(7.74%)
could anyone help me out with where i've gone wrong?谁能帮我解决我哪里出错了?
You need to avoid the first column - which is of character class.您需要避免第一列 - 这是字符类。
findPerc<- function(x){
percent <- as.numeric(round((x/10000) * 100, digits = 2))
paste(x, "(", paste(percent, "%", sep = ''), ")", sep = "")
}
df[, 2:5] <- apply(df[, 2:5],c(1,2), findPerc)
df
# category group1 group2 group3 groupall
# 1 a 1535(15.35%) 4(0.04%) 0(0%) 1539(15.39%)
# 2 b 768(7.68%) 6(0.06%) 0(0%) 774(7.74%)
# 3 c 112(1.12%) 1(0.01%) 0(0%) 113(1.13%)
# 4 d 279(2.79%) 4(0.04%) 0(0%) 283(2.83%)
# 5 e 231(2.31%) 5(0.05%) 0(0%) 236(2.36%)
Instead of c(1,2)
, apply the function to the entire column vector like this而不是
c(1,2)
,像这样将函数应用于整个列向量
apply(df[, 2:5], 2, findPerc)
For applying function to the first two rows, you can do like this要将函数应用于前两行,您可以这样做
apply(df[1:2, 2:5], 2, findPerc)
The error occurs because you're trying to coerce your category
column to a numeric value.发生错误是因为您试图将
category
列强制为数值。 A second problem is that apply
coerces all selected margins to the same type, in this case to character.第二个问题是
apply
所有选定的边距强制为同一类型,在这种情况下为字符。 Observe:观察:
apply(df, 2, mode)
category group1 group2 group3 groupall
"character" "character" "character" "character" "character"
And if you exclude the category
column:如果您排除
category
列:
> apply(df[,2:4], 2, mode)
group1 group2 group3
"numeric" "numeric" "numeric"
Since I'm a tidyverse
fanboy, I suggest you use mutate_if
to only convert those that can be converted:由于我是
tidyverse
迷,我建议你使用mutate_if
只转换那些可以转换的:
df %>%
mutate_if(is.numeric, findPerc)
# A tibble: 5 x 5
category group1 group2 group3 groupall
<chr> <chr> <chr> <chr> <chr>
1 a 15.35% 0.04% 0% 15.39%
2 b 7.68% 0.06% 0% 7.74%
3 c 1.12% 0.01% 0% 1.13%
4 d 2.79% 0.04% 0% 2.83%
5 e 2.31% 0.05% 0% 2.36%
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.