简体   繁体   English

如何按数据框中的列名重命名观察结果?

[英]How to rename observations by column name in data frame?

I have a data frame that resembles the following:我有一个类似于以下内容的数据框:

Name             L06          R12       L02
Joe Smith         1            0         0
Dave Thomas       0            1         1
Alex Smith        0            0         1
Eric Jones        1            0         0

I would like observations with a value of 1 to be renamed to the name of their column.我希望将值为 1 的观察值重命名为其列的名称。 Please note that I will only be renaming certain columns (here columns 2-4).请注意,我只会重命名某些列(这里是第 2-4 列)。

Name             L06          R12       L02
Joe Smith         L06          0         0
Dave Thomas       0            R12       L02
Alex Smith        0            0         L02
Eric Jones        L06          0         0

Is this possible?这可能吗?

You could try with imap from purrr :您可以尝试使用purrr中的imap

cols <- 2:4
df[cols] <- purrr::imap(df[cols], ~replace(.x, .x == 1, .y))
df

#        Name L06 R12 L02
#1   JoeSmith L06   0   0
#2 DaveThomas   0 R12 L02
#3  AlexSmith   0   0 L02
#4  EricJones L06   0   0

In base R, you can use Map :在基地 R 中,您可以使用Map

df[cols] <- Map(function(x, y) replace(x, x == 1, y), df[cols], names(df[cols]))

A base R approach一个base R方法

df[df==1] <-t(replicate(nrow(df),colnames(df)))[df==1]

gives,给,

         Name L06 R12 L02
1   Joe Smith L06   0   0
2 Dave Thomas   0 R12 L02
3  Alex Smith   0   0 L02
4  Eric Jones L06   0   0

Does this work:这行得通吗:

> library(dplyr)
> library(tidyr)
> df %>% pivot_longer(-Name, names_to = 'LR', values_to = 'Ones') %>% 
+   mutate(val = case_when(Ones == 1 ~ LR, TRUE ~ NA_character_)) %>% 
    pivot_wider(id_cols = Name, names_from = LR, values_from = val) %>% 
    +   mutate(across(2:4, ~replace_na(., '0')))
    # A tibble: 4 x 4
      Name        L06   R12   L02  
      <chr>       <chr> <chr> <chr>
    1 Joe Smith   L06   0     0    
    2 Dave Thomas 0     R12   L02  
    3 Alex Smith  0     0     L02  
    4 Eric Jones  L06   0     0    
    > 

We can do this in a single line我们可以在一行中完成

library(tidyr)
df1[-1] <- replace_na(names(df1)[-1][NA^(df1[-1] != 1) * col(df1[-1])], 0)

-output -输出

df1
#         Name L06 R12 L02
#1   Joe Smith L06   0   0
#2 Dave Thomas   0 R12 L02
#3  Alex Smith   0   0 L02
#4  Eric Jones L06   0   0

Or using base R或者使用base R

i1 <- df1[-1] == 1
df1[-1][i1] <- names(df1)[-1][col(df1[-1])][i1]

data数据

df1 <- structure(list(Name = c("Joe Smith", "Dave Thomas", "Alex Smith", 
"Eric Jones"), L06 = c(1L, 0L, 0L, 1L), R12 = c(0L, 1L, 0L, 0L
), L02 = c(0L, 1L, 1L, 0L)), class = "data.frame", row.names = c(NA, 
-4L))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM