简体   繁体   English

在 R 中创建基于 dataframe 中的另一列的列

[英]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 reportingsubscalecomposite数据。

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 reportingcomposite的研究,在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 在studysubscalecomposite的研究中。 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.

相关问题 从 dataframe 创建一个向量,该向量根据 r 中的另一列区分值 - Creating a Vector from a dataframe that discriminates values based on another column in r 基于 R 中的另一列创建新列 - Creating a new column based on another column in R 在一个 dataframe 中创建一个列,基于另一个 dataframe 在 R 中的另一列 - Create a column in one dataframe based on another column in another dataframe in R 根据另一个数据框中的列在一个数据框中创建新列 - Creating new column in one dataframe based on column from another dataframe R:使用另一个数据框创建一个新列 - R: Creating a new column using another dataframe 根据r中另一个数据框中的列填充数据框中的列 - Filling a column in a dataframe based on a column in another dataframe in r 如何根据R中另一个dataframe中的列删除列dataframe中的行? - How to delete rows in a column dataframe based on the column in another dataframe in R? 如果语句基于数据框内的另一列:R中 - If statements based on another column within a dataframe: in R 根据 R 中的列值,基于现有 dataframe 创建另一个 dataframe - Create another dataframe based on an existing dataframe based on a column value in R 折叠数据框,创建新列,名称是另一列的唯一值,值基于另一列的值? 在 R - Collapse a dataframe, creating new columns with name being the unique values of another column, and value based on the value of another column? In R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM