R dplyr从具有列名的列中选择值以在单独的列中进行选择 [英] R dplyr choose value from column with column name to choose in a separate column

查看:104
本文介绍了R dplyr从具有列名的列中选择值以在单独的列中进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中包含不同的数据列(例如x,y,z),另一列指定了要选择的列。我想使用dplyr :: mutate(或类似方法)制作一个新列,该列的值对应于选择指定的列中的值。但我想将所有列都放在首位。在我的真实数据中,我还有其他一些带有元数据的列。

Hi I have a data frame with different data columns (say x, y, z ) and another column which specifies which one to choose. I want to use dplyr::mutate (or similar) to make a new column that has the value corresponding to value in the column specified by "choose". But I want to keep all the columns in the first place. In my real data I also have a few other columns with metadata.

示例数据:

library(dplyr)
testdf <- data.frame(x = 1:5, y = 11:15, z = 101:105, choose = c("z","y","x","y","z"))

在我的示例中,可以使用 case_when ,但是在我的实际脚本中,会生成列名和select列,并且它们可能具有不同的值,所以我不想对可能存在的名称进行硬编码。

I can make this work in my example using case_when but in my actual script the column names and choose column are generated and may have different values, so I don't want to hardcode what names there could be.

所需的输出/测试

mutate(testdf, selectedValue = case_when(choose == "x" ~x,
                                     choose == "y"~ y,
                                     choose == "z"~ z, T~NA_integer_))

#>   x  y   z choose selectedValue
#> 1 1 11 101      z           101
#> 2 2 12 102      y            12
#> 3 3 13 103      x             3
#> 4 4 14 104      y            14
#> 5 5 15 105      z           105

reprex软件包(v0.3.0)

Created on 2019-09-18 by the reprex package (v0.3.0)

推荐答案

这是data.table解决方案。在这种情况下,我认为dplyr解决方案不比其他解决方案(特别是基于r和data.table)更具可读性。

Here is a data.table solution. This is a case where I don't think the dplyr solutions are more human readable than other solutions (base r and data.table specifically).

library(data.table)
testdt <- data.table(x = 1:5, y = 11:15, z = 101:105, choose = c("z","y","x","y","z"))
testdt[,selectedValue := get(choose), by = choose]
testdt
#>    x  y   z choose selectedValue
#> 1: 1 11 101      z           101
#> 2: 2 12 102      y            12
#> 3: 3 13 103      x             3
#> 4: 4 14 104      y            14
#> 5: 5 15 105      z           105

reprex包(v0.3.0)

Created on 2019-09-17 by the reprex package (v0.3.0)

这篇关于R dplyr从具有列名的列中选择值以在单独的列中进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆