[英]Normalizing columns in mixed numeric/non-numeric DataFrame with tidyverse (dplyr)?
I frequently need to normalize columns of DataFrames that have a mixture of numeric and non-numeric columns.我经常需要对混合了数字列和非数字列的 DataFrame 列进行规范化。 Sometimes I know the names of the numeric columns, other times not.
有时我知道数字列的名称,有时我不知道。
I have tried what seem to me to be very logical tidy eval methods.我尝试了在我看来非常合乎逻辑的整洁评估方法。 Most don't work.
大多数都不起作用。 I have only found one that does.
我只找到了一个。
Towards better understanding tidy evaluation, could I please have an explanation of why the following either work or don't work?为了更好地理解整洁的评估,我能否解释一下为什么以下工作或不工作?
library(tidyverse)
df = data.frame(
A=runif(10, 1, 10),
B=runif(10, 1, 10),
C=rep(0, 10),
D=LETTERS[1:10]
)
df
#> A B C D
#> 1 2.157171 1.434351 0 A
#> 2 7.746638 6.987983 0 B
#> 3 7.861337 1.528145 0 C
#> 4 8.657990 4.101441 0 D
#> 5 8.307844 5.809815 0 E
#> 6 1.376084 9.202047 0 F
#> 7 7.197999 5.532681 0 G
#> 8 1.878676 1.012917 0 H
#> 9 2.231955 4.572273 0 I
#> 10 4.340488 2.640728 0 J
print("Does normalize columns, but can't handle col of 0s")
#> [1] "Does normalize columns, but can't handle col of 0s"
test = df %>% mutate_if(is.numeric, ~./sum(.))
test %>% select_if(is.numeric) %>% colSums()
#> A B C
#> 1 1 NaN
print("Virtually the same as above, but tries to handle col of 0s, but doesn't work")
#> [1] "Virtually the same as above, but tries to handle col of 0s, but doesn't work"
test = df %>% mutate_if(is.numeric, ~ifelse(sum(.)>0, ./sum(.), 0))
test %>% select_if(is.numeric) %>% colSums()
#> A B C
#> 0.4167949 0.3349536 0.0000000
print("Does normalize columns, but can't handle col of 0s")
#> [1] "Does normalize columns, but can't handle col of 0s"
test = df %>% mutate_if(is.numeric, function(x) x/sum(x))
test %>% select_if(is.numeric) %>% colSums()
#> A B C
#> 1 1 NaN
print("Virtually the same as above, but tries to handle col of 0s, but doesn't work")
#> [1] "Virtually the same as above, but tries to handle col of 0s, but doesn't work"
test = df %>% mutate_if(is.numeric, function(x) ifelse(sum(x)>0, x/sum(x), 0))
test %>% select_if(is.numeric) %>% colSums()
#> A B C
#> 0.4167949 0.3349536 0.0000000
print("Strange error I don't understand")
#> [1] "Strange error I don't understand"
test = df %>% mutate_if(is.numeric, ~apply(., 2, function(x) x/sum(x)))
#> Error in apply(., 2, function(x) x/sum(x)): dim(X) must have a positive length
print("THIS DOES WORK! Why?")
#> [1] "THIS DOES WORK! Why?"
test = df %>% mutate_if(is.numeric, function(x) if(sum(x)>0) x/sum(x))
test %>% select_if(is.numeric) %>% colSums()
#> A B
#> 1 1
Created on 2019-10-29 by the reprex package (v0.3.0)由reprex package (v0.3.0) 于 2019 年 10 月 29 日创建
Ack!确认! Just noticed a huge problem In the last example, that "works", the column of 0s is dropped.
刚刚注意到一个巨大的问题在最后一个示例中,即“有效”,0 列被删除。 I do not understand this at all.
我完全不明白这一点。 I want to keep that column, just not try to normalize it.
我想保留该列,而不是尝试对其进行规范化。
test = df %>% mutate_if(is.numeric, function(x) if(sum(x)>0) x/sum(x))
> test
# A B D
# 1 0.15571120 0.12033237 A
# 2 0.10561824 0.11198394 B
# 3 0.06041408 0.12068372 C
# 4 0.16785724 0.06241538 D
# 5 0.03112945 0.02559354 E
# 6 0.02791520 0.06363215 F
# 7 0.17132200 0.16625761 G
# 8 0.06641540 0.14038458 H
# 9 0.04015548 0.12420858 I
# 10 0.17346171 0.06450813 J
Figured out I need to include else
.想通了我需要包括
else
。
test = df %>% mutate_if(is.numeric, function(x) if(sum(x)>0) {x/sum(x)}else{0})
> test
# A B C D
# 1 0.15571120 0.12033237 0 A
# 2 0.10561824 0.11198394 0 B
# 3 0.06041408 0.12068372 0 C
# 4 0.16785724 0.06241538 0 D
# 5 0.03112945 0.02559354 0 E
# 6 0.02791520 0.06363215 0 F
# 7 0.17132200 0.16625761 0 G
# 8 0.06641540 0.14038458 0 H
# 9 0.04015548 0.12420858 0 I
# 10 0.17346171 0.06450813 0 J
numeric_columns =
df %>%
select_if(is.numeric) %>%
colnames()
test = df %>% mutate_at(numeric_columns, function(x) if (sum(x) > 0) x/sum(x))
> test
# A B C D
# 1 0.15571120 0.12033237 0 A
# 2 0.10561824 0.11198394 0 B
# 3 0.06041408 0.12068372 0 C
# 4 0.16785724 0.06241538 0 D
# 5 0.03112945 0.02559354 0 E
# 6 0.02791520 0.06363215 0 F
# 7 0.17132200 0.16625761 0 G
# 8 0.06641540 0.14038458 0 H
# 9 0.04015548 0.12420858 0 I
# 10 0.17346171 0.06450813 0 J
test = df %>% mutate_if(is.numeric, ~./sum(.))
test %>% select_if(is.numeric) %>% colSums( ,na.rm = T)
test = df %>% mutate_if(is.numeric, function(x) x/sum(x))
test %>% select_if(is.numeric) %>% colSums()
You can handle your problem specifying na.rm = T
such that you don't keep NA
.您可以指定
na.rm = T
来处理您的问题,这样您就不会保留NA
。 They occur because you divide by 0. It is the same thing for the second syntax which does the same.它们的发生是因为你除以 0。对于第二种语法来说也是一样的。
mutate_if
apply for each numeric column the desired operation so for the third one it returns Nan because of 0. mutate_if
为每个数字列应用所需的操作,因此对于第三个它返回 Nan 因为 0。
test = df %>% mutate_if(is.numeric, function(x){ifelse(x > 0, x/sum(x), rep(0, length(x)))})
test %>% select_if(is.numeric) %>% colSums()
test = df %>% mutate_if(is.numeric, function(x) ifelse(sum(x)>0, x/sum(x), 0))
test %>% select_if(is.numeric) %>% colSums()
ifelse returns a value with the same shape as test so in your case because you check 'sum(x) > 0' you return only the first value. ifelse 返回一个与 test 形状相同的值,因此在您的情况下,因为您检查 'sum(x) > 0' 您只返回第一个值。 See:
看:
https://www.rdocumentation.org/packages/base/versions/3.6.1/topics/ifelse https://www.rdocumentation.org/packages/base/versions/3.6.1/topics/ifelse
test = df %>% mutate_if(is.numeric, ~apply(., 2, function(x) x/sum(x)))
Here, it is tricky, mutate_if apply by vector and you want to use apply next but your object is a vector and apply is correct only for object like matrix
or data.frame
with at least two columns.在这里,它很棘手, mutate_if 按向量应用,您想使用 apply 下一个,但您的 object 是一个向量,并且 apply 仅适用于 object 之类的
matrix
或具有至少两列的data.frame
。
test = df %>% mutate_if(is.numeric, function(x) if(sum(x)>0) x/sum(x))
test %>% select_if(is.numeric) %>% colSums()
Indeed it is a right syntax because if
doesn't require to return a specific size of object.实际上这是一个正确的语法,因为
if
不需要返回 object 的特定大小。
However you could also use ifelse
but with a vector condition indeed a sum of positive value isn't nul if at least one element is different from 0.但是,您也可以使用
ifelse
但如果至少有一个元素与 0 不同,那么在向量条件下,正值的总和确实不是 nul。
test = df %>% mutate_if(is.numeric, function(x){ifelse(x > 0, x/sum(x), rep(0, length(x)))})
test %>% select_if(is.numeric) %>% colSums()
I hope it helps you to understand what is going on when error appears.我希望它可以帮助您了解出现错误时发生的情况。 The solution isn't unique.
解决方案不是唯一的。
The reason is: you return something only if your sum is stricly greater than 0. You must specify what to do if not.原因是:只有当你的总和严格大于 0 时,你才会返回一些东西。如果不是,你必须指定要做什么。 Like this for instance:
比如这样:
test = df %>% mutate_if(is.numeric, function(x) if(sum(x)>0){x/sum(x)}else{0})
@Rémi Coulaud already provided a nice explanation of why things work/don't work. @Rémi Coulaud 已经很好地解释了为什么工作/不工作。 Now, a different way to deal with this problem could be (updated based on remarks from @42-):
现在,处理这个问题的另一种方法可能是(根据@42-的评论更新):
df %>%
mutate_if(~ is.numeric(.) && sum(.) != 0, ~ ./sum(.))
A B C D
1 0.15735803 0.12131787 0 A
2 0.08098114 0.10229536 0 B
3 0.06108911 0.09802935 0 C
4 0.13152492 0.15719599 0 D
5 0.10684839 0.10477812 0 E
6 0.14204157 0.10385447 0 F
7 0.09731823 0.11015997 0 G
8 0.15532621 0.10458007 0 H
9 0.02579446 0.05748756 0 I
10 0.04171793 0.04030124 0 J
And then:接着:
df %>%
mutate_if(~ is.numeric(.) && sum(.) != 0, ~ ./sum(.)) %>%
select_if(is.numeric) %>%
colSums()
A B C
1 1 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.