簡體   English   中英

使用 R 在 dataframe 中重新編碼多個變量的最短和最干凈的方法是什么?

[英]What is the shortest and cleanest way to recode multiple variables in a dataframe using R?

所以我在社會科學領域工作,我經常要做的就是操縱多個變量來改變值。 這通常意味着顛倒比例。 我已經使用SPSS很長時間了,那里的語法非常簡單。 要更改您編寫的多個變量的值:

RECODE var1 var2 var3 (1=5) (2=4) 4=2) (5=1) (ELSE=COPY).

要在新變量中寫入新代碼,請添加into newvar1 newvar1 newvar3. 在最后。 在括號中,您可以使用hilo1 to 4等。

現在我正在努力進入R並且我正在努力尋找執行類似工作流程的最佳方法。 我找到了以下解決方案,但找不到簡短的好方法:

## Packages -----
library(dplyr)
library(car)

## Data -----
tib <- tibble(v1 = 1:4, 
              v2 = 1:4,
              v3 = sample(1:5, 4, replace = FALSE))

vars <- c("v1", "v2", "v3")

基礎方式:

tib$v2_rec <- NA
tib$v2_rec[tib$v2 == 1] <- 5 #1
tib$v2_rec[tib$v2 == 2] <- 4 #2
tib$v2_rec[tib$v2 == 3] <- 3 #3
tib$v2_rec[tib$v2 == 4] <- 2 #4
tib$v2_rec[tib$v2 == 5] <- 1 #5
# I'm forced to create a new variable here, otherwise #4 and #5 overwrite #1 and #2.
# Therefore I won't even bother to try to loop trough multiple variables.

來自 package 汽車的 recode():

tib$v1 <- recode(tib$v1, "1=5; 2=4; 4=2; 5=1")
# This is nice, understandable and short
# To handle multiple variables the following solutions won't work, because the reload functions seems not to be able to iterate through lists:

tib[vars] <- recode(tib[vars], "1=5; 2=4; 4=2; 5=1")
tib[1:3] <- recode(tib[1:3], "1=5; 2=4; 4=2; 5=1")

# I'd be forced to loop:

for (i in vars) {
  tib[[i]] <- recode(tib[[i]], "1=5; 2=4; 4=2; 5=1")
}

我對此很滿意,但我想知道是否有 function 可以為我完成循環工作。 我現在真的在 dplyer 功能上苦苦掙扎,我很不高興我無法直觀地弄清楚事情......

我試過變異:

#I get it for a single case and for multiple cases i got to a solution in combination with the recode() function:

tib <- tib %>%
  mutate_at(vars(v1:v3), 
            function(x) recode(x, "1=5; 2=4; 4=2; 5=1"))

這是最好的方法嗎? 為了清楚起見,我看到了一些其他使用 case_when()、replace() 或 mapvalues() 的解決方案,但我發現上面的解決方案更好,因為我喜歡一眼就看到什么值被重新編碼為什么值。

我對 apply() function 有了一點了解,甚至無法用它重新編碼一個變量。 我相信我也會很快掌握這一點,但目前我只是有點沮喪,我在 SPSS 中尋找工作流程花了我多長時間。 如果您知道任何比上述使用 apply() function 的解決方案更短、更清潔的解決方案,我將非常感激!

我對 R 和它的可能性感到滿意,但現在我需要一個正確方向的提示來讓我繼續前進! 先感謝您!

我認為如果使用正確, dplyr在這種情況下具有“最干凈”的語法:

library(dplyr)
tib <- tibble(v1 = 1:4, 
              v2 = 1:4,
              v3 = sample(1:5, 4, replace = FALSE))

tib %>% 
  mutate_at(vars(v1:v3), recode, `1` = 5, `2` = 4, `3` = 3, `4` = 2, `5` = 1)
#> # A tibble: 4 x 3
#>      v1    v2    v3
#>   <dbl> <dbl> <dbl>
#> 1     5     5     2
#> 2     4     4     5
#> 3     3     3     4
#> 4     2     2     1

請注意,我必須添加3 = 3因為 recode 需要替換所有值。

我經常發現使用對我來說新的函數更明確地編寫東西更容易,所以也許這可能會有所幫助:

tib %>% 
  mutate_at(.vars = vars(v1:v3), 
            .funs = function(x) recode(x, 
                                       `1` = 5, 
                                       `2` = 4, 
                                       `3` = 3, 
                                       `4` = 2, 
                                       `5` = 1))

如果您更喜歡從car recode function,您不應該加載car ,而是使用:

tib %>% 
  mutate_at(vars(v1:v3), car::recode, "1=5; 2=4; 4=2; 5=1")

這樣你就不會在將dplyrcar混合時遇到麻煩(只要你不需要car來做其他事情。

這是僅使用基本函數的簡單方法。 這假設這些是原始編碼為 1 - 5 的 5 點 Likert 項目。例如,如果您有 7 點 Likert 項目,或編碼為 0 - 4 或 -2 - 2,則需要調整它.

一些編碼說明:您的數據集有一個偽隨機生成元素(對sample()的調用); 要使數據集完全可重現,請使用?set.seed 使用箭頭賦值運算符 ( (var <- value) ) 時,您可以通過將其括在括號中來自動打印已賦值的變量或數據集。 R 是矢量化的,所以你不需要循環(雖然這里真的沒問題——變量很少,不會導致明顯的減速)。

set.seed(4636)  # this makes the example exactly reproducible
(d <- data.frame(v1 = 1:4, 
                 v2 = 1:4,
                 v3 = sample(1:5, 4, replace = FALSE)))  # adding outer ()'s prints
#   v1 v2 v3
# 1  1  1  1
# 2  2  2  2
# 3  3  3  5
# 4  4  4  4

d.orig <- d  # here's your original dataset, so they aren't overwritten
(d <- 6-d)  # adding outer ()'s prints
#   v1 v2 v3
# 1  5  5  5
# 2  4  4  4
# 3  3  3  1
# 4  2  2  2

rec.vars <- c("v2")
d.some   <- d.orig
(d.some[,rec.vars] <- 6-d.some[,rec.vars])
# [1] 5 4 3 2
d.some
#   v1 v2 v3
# 1  1  5  1
# 2  2  4  2
# 3  3  3  5
# 4  4  2  4

##### to do more than 1 variable
(rec.vars <- paste0("v", c(2,3)))
# [1] "v2" "v3"
d.some   <- d.orig
(d.some[,rec.vars] <- 6-d.some[,rec.vars])
#   v2 v3
# 1  5  5
# 2  4  4
# 3  3  1
# 4  2  2
d.some
#   v1 v2 v3
# 1  1  5  5
# 2  2  4  4
# 3  3  3  1
# 4  4  2  2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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