【发布时间】:2020-02-25 03:37:06
【问题描述】:
我经常需要对混合了数字列和非数字列的 DataFrame 列进行规范化。有时我知道数字列的名称,有时我不知道。
我尝试了在我看来非常合乎逻辑的整洁评估方法。大多数都不起作用。我只找到了一个。
为了更好地理解整洁的评估,我能否解释一下为什么以下工作或不工作?
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
由reprex package (v0.3.0) 于 2019 年 10 月 29 日创建
编辑!!!
确认!刚刚发现一个大问题 在最后一个示例中,即“有效”,删除了 0 列。我完全不明白这一点。我想保留该列,只是不尝试对其进行规范化。
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
编辑 2
发现我需要包含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
【问题讨论】:
标签: r dplyr tidyverse tidyeval