簡體   English   中英

使用`dplyr`有條件地改變列值

[英]conditionally mutating column values using `dplyr`

我正在使用WRS2進行穩健的成對比較。 但一個問題是它從輸出數據幀中刪除了組級別名稱並將其保存在不同的對象中。

# setup
set.seed(123)
library(WRS2)
library(tidyverse)

# robust pairwise comparisons
x <- lincon(libido ~ dose, data = viagra, tr = 0.1)

# comparisons
x$comp
#>      Group Group psihat  ci.lower    ci.upper    p.value
#> [1,]     1     2   -1.0 -3.440879  1.44087853 0.25984505
#> [2,]     1     3   -2.8 -5.536161 -0.06383861 0.04914871
#> [3,]     2     3   -1.8 -4.536161  0.93616139 0.17288911

# vector with group level names
x$fnames
#> [1] "placebo" "low"     "high"

我可以將其轉換為tibble

# converting to tibble
suppressMessages(as_tibble(x$comp, .name_repair = "unique")) %>%
  dplyr::rename(group1 = Group...1, group2 = Group...2) 
#> # A tibble: 3 x 6
#>   group1 group2 psihat ci.lower ci.upper p.value
#>    <dbl>  <dbl>  <dbl>    <dbl>    <dbl>   <dbl>
#> 1      1      2   -1      -3.44   1.44    0.260 
#> 2      1      3   -2.8    -5.54  -0.0638  0.0491
#> 3      2      3   -1.8    -4.54   0.936   0.173

然后,我想用fnames包含的實際名稱替換group列數值(因此映射fnames [1] -> 1、 fnames [2] -> 2,依此類推)。

所以最終的數據幀應該看起來像下面這樣——

#> # A tibble: 3 x 6
#>   group1 group2 psihat ci.lower ci.upper p.value
#>    <dbl>  <dbl>  <dbl>    <dbl>    <dbl>   <dbl>
#> 1      placebo      low   -1      -3.44   1.44    0.260 
#> 2      placebo      high   -2.8    -5.54  -0.0638  0.0491
#> 3      low      high   -1.8    -4.54   0.936   0.173

在這種情況下,復制粘貼這三個值很容易,但我想要一種通用的方法,無論級別數如何,它都可以工作。 我怎樣才能使用dplyr做到這dplyr

這是否滿足您的需求:

names <- c("A","B","C")

df = data.frame(group=c(1,2,3))
library(dplyr)
df %>% mutate(group = names[group])

  group
1     A
2     B
3     C

這是一種使用recode函數的方法,其中重新編碼向量是根據數據以編程方式構建的:

# Setup
set.seed(123)
library(WRS2)
library(tidyverse)

x <- lincon(libido ~ dose, data = viagra, tr = 0.1)

# Create recoding vector
recode.vec = x$fnames %>% set_names(1:length(x$fnames))

# Recode columns
x.comp = x$comp %>% 
  as_tibble(.name_repair=make.unique) %>% 
  mutate(across(starts_with("Group"), ~recode(., !!!recode.vec)))

輸出:

x.comp

#> # A tibble: 3 x 6
#>   Group   Group.1 psihat ci.lower ci.upper p.value
#>   <chr>   <chr>    <dbl>    <dbl>    <dbl>   <dbl>
#> 1 placebo low       -1      -3.44   1.44    0.260 
#> 2 placebo high      -2.8    -5.54  -0.0638  0.0491
#> 3 low     high      -1.8    -4.54   0.936   0.173

使用命名向量與tidyverse匹配。 這按值而不是按索引序列匹配,即如果“組”列中的值不在序列或字符中,這仍然有效

library(dplyr)
as_tibble(x$comp, .name_repair = 'unique') %>%
   mutate(across(starts_with("Group"), 
         ~ setNames(x$fnames, seq_along(x$fnames))[as.character(.)]))

在將對象提取為 tibbles 后,嘗試使用這種tidyverse方法將數據格式化為長時間。 您可以根據需要使用left_join()來獲取您的組。 這里是獲得接近你想要的東西的代碼:

# setup
set.seed(123)
library(WRS2)
library(tidyverse)
# robust pairwise comparisons
x <- lincon(libido ~ dose, data = viagra, tr = 0.1)
#Transform to tibble
df1 <- suppressMessages(as_tibble(x$comp, .name_repair = "unique")) %>%
  dplyr::rename(group1 = Group...1, group2 = Group...2) 
#Extract labels
df2 <- tibble(treat=x$fnames) %>% mutate(value=1:n())
#Format to long df1
df1 <- df1 %>% 
  mutate(id=1:n()) %>%
  pivot_longer(cols = c(group1,group2)) %>%
  rename(group=name) %>% left_join(df2) %>% select(-value) %>%
  pivot_wider(names_from = group,values_from=treat) %>% select(-id)

輸出:

# A tibble: 3 x 6
  psihat ci.lower ci.upper p.value group1  group2
   <dbl>    <dbl>    <dbl>   <dbl> <chr>   <chr> 
1   -1      -3.44   1.44    0.260  placebo low   
2   -2.8    -5.54  -0.0638  0.0491 placebo high  
3   -1.8    -4.54   0.936   0.173  low     high  

暫無
暫無

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

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