简体   繁体   English

如何用R中的条件语句替换行值

[英]how to replace row values with conditional statement in R

I'm having 3 columns Seq, Period and Treatment. 我有3列Seq,Period和Treatment。 Sequence has values ABB,BBA,BAB,.. ie 1A and 2B combination. 序列具有值ABB,BBA,BAB ..即1A和2B组合。 Period has 3 values ie 1,2 & 3 and Treatment will have value based on the Period and Sequence eg. 周期具有3个值,即1,2和3,处理将具有基于周期和顺序的值,例如。 if Sequence is ABB and Period is 1 then Treatment will be A if Sequence is ABB and Period is 2 then Treatment will be B similarly if Sequence is ABB and Period is 3 then Treatment will be B. following is the snapshot of the data 如果Sequence为ABB并且Period为1,则处理将为A;如果Sequence为ABB并且Period为2,则处理将为B;如果Sequence为ABB并且Period为3,则处理将为B。以下是数据的快照

 Sequence Period Treatment   
    BBA      1         B  
    BBA      2         B  
    BBA      3         A 
    ABB      1         A 
    ABB      2         B 
    ABB      3         B

I want to Replace Treatment Value B of Sequence BBA and Period 1 with B1 and Sequence BBA and Period 2 with B2 similarly with the rest of the data. 我想用其余的数据类似地用B1替换序列BBA和周期1的处理值B,并用B2替换序列BBA和周期2的处理值。

I tried the following code 我尝试了以下代码

updatedata<-if((data$Sequence=='ABB' && data$Period==2) || (data$Sequence=='BAB' && data$Period==1) || (data$Sequence=='BBA' && data$Period==1)){
  data$Treatment<-'B1'
}else if((data$Sequence=='ABB' && data$Period==3) || (data$Sequence=='BAB' && data$Period==3) || (data$Sequence=='BBA' && data$Period==2)){
data$Treatment<-'B2'
}else((data$Sequence=='ABB' && data$Period==1) || (data$Sequence=='BAB' && data$Period==2) || (data$Sequence=='BBA' && data$Period==3)){
data$Treatment<-'A'}

I expect the following results 我期望以下结果

Sequence Period Treatment   
    BBA      1         B1  
    BBA      2         B2 
    BBA      3         A 
    ABB      1         A 
    ABB      2         B1
    ABB      3         B2

But I'm getting the following error: 但我收到以下错误:

Error: unexpected '{' in: " data$Treatment<-'B2' }else((data$Sequence=='ABB' && data$Period==1) || (data$Sequence=='BAB' && data$Period==2) || (data$Sequence=='BBA' && data$Period==3)){" 错误:意外的'{'in:“ data $ Treatment <-'B2'} else((data $ Sequence =='ABB'&& data $ Period == 1)||(data $ Sequence =='BAB'&& data $ Period == 2)||(data $ Sequence =='BBA'&& data $ Period == 3)){“

Using dplyr 使用dplyr

z%>%mutate(Treatment=if_else(Treatment=="B",paste0("B",Period),Treatment))

1      BBA      1        B1
2      BBA      2        B2
3      BBA      3         A
4      ABB      1         A
5      ABB      2        B2
6      ABB      3        B3

Edit 编辑

Second version: 第二版:

cond1 = z$Sequence=="BBA" & z$Treatment=="B"
cond2 = z$Sequence=="ABB" & z$Treatment=="B"
cond3 = z$Sequence=="BAB" & z$Treatment=="B"

z$Treatment[cond1]=paste0(z$Treatment[cond1],z$Period[cond1])
z$Treatment[cond2]=paste0(z$Treatment[cond2],z$Period[cond2]-1)
z$Treatment[cond3]=paste0(z$Treatment[cond3],
                          ifelse(z$Period[cond3]>2,2,1))
> z
   Sequence Period Treatment
2       BBA      1        B1
3       BBA      2        B2
4       BBA      3         A
5       ABB      1         A
6       ABB      2        B1
7       ABB      3        B2
8       BAB      1        B1
9       BAB      2         A
10      BAB      3        B2

data: 数据:

structure(list(Sequence = c("BBA", "BBA", "BBA", "ABB", "ABB", 
"ABB", "BAB", "BAB", "BAB"), Period = c(1, 2, 3, 1, 2, 3, 1, 
2, 3), Treatment = c("B1", "B2", "A", "A", "B1", "B2", "B1", 
"A", "B2")), row.names = 2:10, class = "data.frame")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM