R:如果主题的某一列具有特定值,如何标记主题 [英] R : How to tag a subject if one of their columns has a certain value

查看:60
本文介绍了R:如果主题的某一列具有特定值,如何标记主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我的数据:

+---------+--+----------+--+
| Subj_ID |  | Location |  |
+---------+--+----------+--+
|       1 |  |        1 |  |
|       1 |  |        2 |  |
|       1 |  |        3 |  |
|       2 |  |        1 |  |
|       2 |  |        4 |  |
|       2 |  |        2 |  |
|       3 |  |        1 |  |
|       3 |  |        2 |  |
|       3 |  |        5 |  |
+---------+--+----------+--+

在此数据集中,只有主题1的位置值为3,因此我想将主题1标记为是以进行干预。由于主题2和3的位置值都不为3,因此需要将其标记为false。

In this dataset, only subject 1 has a location value of 3, so I want to label subject 1 as YES for intervention. Since subject 2 and 3 didn't have a location value of 3, they need to be labeled as false.

这就是我希望数据显示的样子。

This is what I want the data to look like.

| Subj_ID |  | Location | Intervention |
+---------+--+----------+--------------+
|       1 |  |        1 | YES |
|       1 |  |        2 | YES |
|       1 |  |        3 | YES |
|       2 |  |        1 | NO  |
|       2 |  |        4 | NO  |
|       2 |  |        3 | NO  |
|       3 |  |        1 | NO  |
|       3 |  |        2 | NO  |
|       3 |  |        5 | NO  |
+---------+--+----------+-----+

在此先感谢您的帮助!如果可能,首选Dplyr。

Thanks in advance for the help! Dplyr preferred if possible.

推荐答案

带有 dplyr 的选项是按'Subj_ID'分组后的,检查3是否为%in /%返回单个TRUE / FALSE的位置,将其更改为数字索引以将值替换为 NO, YES

An option with dplyr is after grouping by 'Subj_ID', check whether 3 is %in/% Location which returns a single TRUE/FALSE, change that to a numeric index to replace the values with "NO", "YES"

library(dplyr)
df1 %>%
   group_by(Subj_ID) %>%
   mutate(Intervention =  c("NO", "YES")[(3 %in% Location)+1])
# A tibble: 9 x 3
# Groups:   Subj_ID [3]
#  Subj_ID Location Intervention
#    <int>    <dbl> <chr>       
#1       1        1 YES         
#2       1        2 YES         
#3       1        3 YES         
#4       2        1 NO          
#5       2        4 NO          
#6       2        2 NO          
#7       3        1 NO          
#8       3        2 NO          
#9       3        5 NO        






或使用任何

df1 %>%
   group_by(Subj_ID) %>%
   mutate(Intervention = case_when(any(Location == 3) ~ "YES", TRUE ~ "NO"))






或使用 base R

df1$Intervention <- with(df1, c("NO", "YES")[1 + (Subj_ID %in% 
             Subj_ID[Location == 3])])



数据



data

df1 <- data.frame(Subj_ID = rep(1:3, each = 3),
             Location = c(1:3, 1, 4, 2, 1, 2, 5))

这篇关于R:如果主题的某一列具有特定值,如何标记主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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