在我的data,我有一些studyIESreporting都subscale和composite。
我想添加一個名為include. 對于studyIESreporting既subscale和composite,在那些行subscale,include應該是TRUE,否則它必須是虛假的; 任何其他行都必須為 TRUE。
換句話說,只有在同時報告和 的ie 中include才能為 FALSE 。其他任何地方都必須是 TRUE。reporting==compositestudysubscalecompositeinclude
我想要的輸出如下。這是可以實作的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"
uj5u.com熱心網友回復:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/378679.html
上一篇:R中資料幀中的嵌套子集
