[英]creating a column that based on another column in dataframe in R
In my data
, I've some study
ies reporting
both subscale
and composite
.在我的data
中,我有一些study
reporting
了subscale
和composite
数据。
I want to add a new column called include
.我想添加一个名为include
的新列。 For study
ies reporting
both subscale
and composite
, in the rows that are subscale
, include
should be TRUE else it must be FALSE;对于同时study
reporting
和composite
的研究,在subscale
include
subscale
为 TRUE,否则它必须为 FALSE; any other row must be TRUE.任何其他行必须为 TRUE。
In other words, include
can only be FALSE for reporting==composite
ONLY in study
ies that have reported both subscale
and composite
.换句话说, include
只能是 FALSE for reporting==composite
ONLY 在study
了subscale
和composite
的研究中。 Everywhere else include
must be TRUE.其他地方include
必须为 TRUE。
My desired output is below.我想要的 output 如下。 Is this achievable in R
?这在R
中可以实现吗?
library(tidyverse)
m="
study reporting
1 subscale
1 composite
2 subscale
2 composite
3 composite
3 composite
4 composite
5 subscale"
data <- read.table(text = m, h=T)
desired =
"study reporting include
1 subscale TRUE
1 composite FALSE
2 subscale TRUE
2 composite FALSE
3 composite TRUE
3 composite TRUE
4 composite TRUE
5 subscale TRUE"
library(dplyr)
data %>%
group_by(study) %>%
mutate(
include = !(
"subscale" %in% reporting &
"composite" %in% reporting &
reporting == "composite"
))
# # A tibble: 8 × 3
# # Groups: study [5]
# study reporting include
# <int> <chr> <lgl>
# 1 1 subscale TRUE
# 2 1 composite FALSE
# 3 2 subscale TRUE
# 4 2 composite FALSE
# 5 3 composite TRUE
# 6 3 composite TRUE
# 7 4 composite TRUE
# 8 5 subscale TRUE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.