簡體   English   中英

使用 tidyverse (dplyr) 對混合數字/非數字 DataFrame 中的列進行規范化?

[英]Normalizing columns in mixed numeric/non-numeric DataFrame with tidyverse (dplyr)?

我經常需要對混合了數字列和非數字列的 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

第一個問題

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()

您可以指定na.rm = T來處理您的問題,這樣您就不會保留NA 它們的發生是因為你除以 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 返回一個與 test 形狀相同的值,因此在您的情況下,因為您檢查 'sum(x) > 0' 您只返回第一個值。 看:

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)))

在這里,它很棘手, 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()

實際上這是一個正確的語法,因為if不需要返回 object 的特定大小。

但是,您也可以使用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()

我希望它可以幫助您了解出現錯誤時發生的情況。 解決方案不是唯一的。

編輯1:

原因是:只有當你的總和嚴格大於 0 時,你才會返回一些東西。如果不是,你必須指定要做什么。 比如這樣:

test = df %>% mutate_if(is.numeric, function(x) if(sum(x)>0){x/sum(x)}else{0})

@Rémi Coulaud 已經很好地解釋了為什么工作/不工作。 現在,處理這個問題的另一種方法可能是(根據@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

接着:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM