简体   繁体   English

ifelse() 使用 mutate 无法正常工作:R

[英]ifelse() not working correctly using mutate: R

I have a small doubt in ifelse confition.我对 ifelse confition 有一点怀疑。 I've read numerous articles and tried different solutions but I'm not able to solve it.我已经阅读了许多文章并尝试了不同的解决方案,但我无法解决它。 Please help me with it.请帮帮我。

I have a column of numbers ranging from 0:59 and it recurs like this for 500 rows.我有一列从 0:59 开始的数字,它像这样重复出现 500 行。 I am trying to create another column which divides 0:29 as 0 and 30:59 as 30.我正在尝试创建另一列,将 0:29 划分为 0,将 30:59 划分为 30。

Data:数据:

> data$Minute
   [1]  0  0  0  1  1  1  2  2  2  2  2  2  3  3  4  4  4  5  5  5  5  5  6  6  6  7  7  7  7  7  8  8  8  8  8  8  9
  [38] 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 11 11 12 12 12 12 13 13 13 13 13 14 14 14 14 14 14 15 15 15
  [75] 15 15 15 15 15 15 16 16 16 16 16 17 17 18 18 18 18 18 19 19 19 19 20 20 20 20 20 20 20 20 21 21 21 21 21 21 21
 [112] 22 22 22 22 22 23 23 23 23 23 23 23 23 23 23 23 23 23 23 24 24 24 24 24 24 24 24 24 25 25 25 25 25 25 25 25 25
 [149] 26 26 26 26 26 26 26 26 26 26 27 27 27 27 27 27 27 27 27 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 29
 [186] 29 29 29 29 29 29 29 29 29 29 29 30 30 30 30 30 30 30 30 30 30 30 30 30 31 31 31 31 31 31 31 31 31 31 31 31 31
 [223] 31 32 32 32 32 32 33 33 33 33 33 33 33 33 33 33 33 34 34 34 34 34 34 34 34 34 34 34 34 34 35 35 35 35 35 35 35
 [260] 35 35 35 36 36 36 36 36 36 36 36 37 37 37 37 37 37 37 37 37 38 38 38 38 38 38 38 38 38 38 39 39 39 39 39 39 39
 [297] 39 39 40 40 40 40 40 40 40 40 40 40 40 40 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 42 42 42 42 42 42 42
 [334] 42 42 42 42 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 44 44 44 44 44 44 44 44 44 45 45 45 45 45 45 45 45
 [371] 45 45 45 45 45 45 45 45 46 46 46 46 46 46 46 47 47 47 47 47 47 47 47 47 47 47 47 47 48 48 48 48 48 48 48 48 48
 [408] 48 48 48 49 49 49 49 49 49 49 50 50 50 50 50 50 50 50 50 50 50 50 50 50 51 51 51 51 51 51 51 51 51 51 52 52 52
 [445] 52 52 52 52 52 52 52 52 52 53 53 53 53 53 53 53 53 54 54 54 54 54 54 54 54 54 54 54 55 55 55 55 55 55 55 55 55
 [482] 55 56 56 56 56 56 56 56 56 56 56 56 56 56 57 57 57 57 57 57 57 57 58 58 58 58 58 58 58 58 58 59 59 59 59 59 59
 [519] 59 59 59 59 59 59 59  0  0  0  0 

Code:代码:

data<- data %>%  mutate(Period = ifelse(((Minute >=0) && (Minute <= 30)),0, 30) )           

While running this code, I am only getting Period value as 0 for all the data points.在运行此代码时,我只将所有数据点的 Period 值设为 0。 Can you please help me with this small issue?你能帮我解决这个小问题吗?

Thanks in Advance!提前致谢!

Try this尝试这个

data %>%  mutate(Period = ifelse(minute <= 30, 0, 30))

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

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