为什么这个 - 否则不起作用?

时间:2014-11-29 14:15:19

标签: r if-statement

如何根据var2和var1更改var3的值?我想在var2 = 0时将var3的值加倍。例如对于var1 = A,当var2 = 0时我想要var3 = 2 * 10000.我尝试了下面的代码,这是错误的。有人可以帮忙吗?谢谢!

dat2 <- data_frame(
     var1 = factor(rep(dat1$var1, each=2)),
     var2 = factor(rep(0:1, 8) ),
     var3 = rep(dat1$var3*ifelse(dat1$var2==0, 2, 1), each =1)
     )

     Var1   var2   var3
1       A      0  10000    
2       A      1  10000
3       B      0  15000
4       B      1  15000
5       C      0  18000
6       C      1  18000
7       D      0  12500
8       D      1  12500
9       E      0  19500
10      E      1  19500
11      F      0  15500    
12      F      1  15500    
13      G      0  20500    
14      G      1  20500    
15      H      0  10500    
16      H      1  10500

2 个答案:

答案 0 :(得分:1)

不确定我完全理解作业规则。看哪一个适合。

# the sample data
df = structure(list(Var1 = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 
4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L), .Label = c("A", "B", "C", 
"D", "E", "F", "G", "H"), class = "factor"), var2 = c(0L, 1L, 
0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L), var3 = c(10000L, 
10000L, 15000L, 15000L, 18000L, 18000L, 12500L, 12500L, 19500L, 
19500L, 15500L, 15500L, 20500L, 20500L, 10500L, 10500L)), .Names = c("Var1", 
"var2", "var3"), class = "data.frame", row.names = c("1", "2", 
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", 
"15", "16"))

# assumption 1 : only double var3 when var2 == 0
df$var3_new <- ifelse(df$var2 == 0, df$var3*2, df$var3)

# assumption 2 : only double var3 when Var1 == "A" & var2 == 0
df$var3_new <- ifelse(df$Var1 == "A" & df$var2 == 0,
                      df$var3*2, df$var3)

# Here I assign the updated values to var3_new column so that you could
# compare.  You could change var3_new to var3 so that var3 will be
# re-written with new values according to your formula.  Happy
# exploration.

# As to ifelse, basically the logic is:
# ifelse(condition_1, 
#        assign a value if condition_1 is satisfied, 
#        assign other values otherwise)

最后,建议在此网站中搜索ifelse。应该有充足的例子。

答案 1 :(得分:1)

您可以在不使用ifelse

的情况下执行此操作
 indx <- !dat2$var2
 dat2$var3[indx] <- 2*dat2$var3[indx]
 dat2
 #   Var1 var2  var3
 #1     A    0 20000
 #2     A    1 10000
 #3     B    0 30000
 #4     B    1 15000
 #5     C    0 36000
 #6     C    1 18000
 #7     D    0 25000
 #8     D    1 12500
 #9     E    0 39000
 #10    E    1 19500
 #11    F    0 31000
 #12    F    1 15500
 #13    G    0 41000
 #14    G    1 20500
 #15    H    0 21000
 #16    H    1 10500

或者

transform(dat2, var3= ((!var2)+1)*var3)
#    Var1 var2  var3
#1     A    0 20000
#2     A    1 10000
#3     B    0 30000
#4     B    1 15000
#5     C    0 36000
#6     C    1 18000
#7     D    0 25000
#8     D    1 12500
#9     E    0 39000
#10    E    1 19500
#11    F    0 31000
#12    F    1 15500
#13    G    0 41000
#14    G    1 20500
#15    H    0 21000
#16    H    1 10500

数据

dat2 <- structure(list(Var1 = c("A", "A", "B", "B", "C", "C", "D", "D", 
"E", "E", "F", "F", "G", "G", "H", "H"), var2 = c(0L, 1L, 0L, 
1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L), var3 = c(10000L, 
10000L, 15000L, 15000L, 18000L, 18000L, 12500L, 12500L, 19500L, 
19500L, 15500L, 15500L, 20500L, 20500L, 10500L, 10500L)), .Names = c("Var1", 
 "var2", "var3"), class = "data.frame", row.names = c("1", "2", 
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", 
"15", "16"))