New variable with mutate and ifelse

I need to create a new variable called Valence that is a value from 0:2. For Participants from 1 to 41, Valence value should have a sequence from 0:2, but for participants for Participants from 41:44 the Valence should be 0. How could I do it with a line of code? This is what I have tried, and it has not worked.

Participant <- seq(0:44) %>%
  as.data.frame()  %>%
  mutate(Valence = ifelse(Participant = 1:41, rep(0:2), 0))
1 Like

It works...Just needed to add a =.

Participant <- seq(0:44) %>%
  as.data.frame()  %>%
  mutate(Valence = ifelse(Participant == 1:41, rep(0:2), 0))

I was about to say that what you want can't be done, as a particular column of a data.frame has to be a single number, and it can't be a sequence.

If you've figured it out, that's great. Can you please provide your complete code? What you've posted doesn't run on my device, as Participant is not defined as yet.

@Yarnabrina
Heres a reproducible example:

  L <- seq(1:44) %>%
  as.data.frame()  %>%
  mutate(Participant = row_number()) %>%
  mutate(Valence = ifelse(Participant == 1:41, rep(0:2), 0))

It gives me a warning message (so probably there's a better workaround) but it does de job.

Warning message:
In Participant == 1:41 :
  longer object length is not a multiple of shorter object length

Just try %in% :wink:

L <- seq(1:44) %>%
  as.data.frame()  %>%
  mutate(Participant = row_number()) %>%
  mutate(Valence = ifelse(Participant %in% 1:41, rep(0:2), 0))
2 Likes

If you meant that you want repetition of the sequence 0:2 for 1:41 as a whole, then don't read further. I thought you meant one of 0, 1, 2 for 1:41. (I'm not sure whether I'm able to express what I think, as English isn't my 1st language, but hopefully you guys will understand)

Otherwise, check the reprex:

# loading library
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# what you (OP) did

## repeats 0:2 for 1:41
## uses 0 for rest
## warning occurs because you're comparing one element with 41 elements
L1 <- seq(1:44) %>%
  as.data.frame()  %>%
  mutate(Participant = row_number()) %>%
  mutate(Valence = ifelse(Participant == 1:41, rep(0:2), 0))
#> Warning in Participant == 1:41: longer object length is not a multiple of
#> shorter object length
L1
#>     . Participant Valence
#> 1   1           1       0
#> 2   2           2       1
#> 3   3           3       2
#> 4   4           4       0
#> 5   5           5       1
#> 6   6           6       2
#> 7   7           7       0
#> 8   8           8       1
#> 9   9           9       2
#> 10 10          10       0
#> 11 11          11       1
#> 12 12          12       2
#> 13 13          13       0
#> 14 14          14       1
#> 15 15          15       2
#> 16 16          16       0
#> 17 17          17       1
#> 18 18          18       2
#> 19 19          19       0
#> 20 20          20       1
#> 21 21          21       2
#> 22 22          22       0
#> 23 23          23       1
#> 24 24          24       2
#> 25 25          25       0
#> 26 26          26       1
#> 27 27          27       2
#> 28 28          28       0
#> 29 29          29       1
#> 30 30          30       2
#> 31 31          31       0
#> 32 32          32       1
#> 33 33          33       2
#> 34 34          34       0
#> 35 35          35       1
#> 36 36          36       2
#> 37 37          37       0
#> 38 38          38       1
#> 39 39          39       2
#> 40 40          40       0
#> 41 41          41       1
#> 42 42          42       0
#> 43 43          43       0
#> 44 44          44       0

# what Adam did

## same output will be generated as yours
## warning does not occur because he's checking whether one element belongs to 1:41 or not
L2 <- seq(1:44) %>%
  as.data.frame()  %>%
  mutate(Participant = row_number()) %>%
  mutate(Valence = ifelse(Participant %in% 1:41, rep(0:2), 0))
L2
#>     . Participant Valence
#> 1   1           1       0
#> 2   2           2       1
#> 3   3           3       2
#> 4   4           4       0
#> 5   5           5       1
#> 6   6           6       2
#> 7   7           7       0
#> 8   8           8       1
#> 9   9           9       2
#> 10 10          10       0
#> 11 11          11       1
#> 12 12          12       2
#> 13 13          13       0
#> 14 14          14       1
#> 15 15          15       2
#> 16 16          16       0
#> 17 17          17       1
#> 18 18          18       2
#> 19 19          19       0
#> 20 20          20       1
#> 21 21          21       2
#> 22 22          22       0
#> 23 23          23       1
#> 24 24          24       2
#> 25 25          25       0
#> 26 26          26       1
#> 27 27          27       2
#> 28 28          28       0
#> 29 29          29       1
#> 30 30          30       2
#> 31 31          31       0
#> 32 32          32       1
#> 33 33          33       2
#> 34 34          34       0
#> 35 35          35       1
#> 36 36          36       2
#> 37 37          37       0
#> 38 38          38       1
#> 39 39          39       2
#> 40 40          40       0
#> 41 41          41       1
#> 42 42          42       0
#> 43 43          43       0
#> 44 44          44       0

# what I was going to answer

## it assigns 0 to 42:44
## generates a random number from 0, 1, 2 for 1:41
## instead of random generation, you may fix (of course)
set.seed(seed = 26727)
L3 <- seq(1:44) %>%
  as.data.frame()  %>%
  mutate(Participant = row_number()) %>%
  mutate(Valence = ifelse(test = (Participant %in% 42:44),
                          yes = 0,
                          no = sample(x = 0:2,
                                      size = 44,
                                      replace = TRUE)))
L3
#>     . Participant Valence
#> 1   1           1       0
#> 2   2           2       1
#> 3   3           3       2
#> 4   4           4       2
#> 5   5           5       2
#> 6   6           6       2
#> 7   7           7       0
#> 8   8           8       1
#> 9   9           9       0
#> 10 10          10       1
#> 11 11          11       0
#> 12 12          12       1
#> 13 13          13       0
#> 14 14          14       2
#> 15 15          15       2
#> 16 16          16       0
#> 17 17          17       1
#> 18 18          18       2
#> 19 19          19       2
#> 20 20          20       0
#> 21 21          21       2
#> 22 22          22       1
#> 23 23          23       0
#> 24 24          24       0
#> 25 25          25       0
#> 26 26          26       1
#> 27 27          27       2
#> 28 28          28       1
#> 29 29          29       0
#> 30 30          30       0
#> 31 31          31       0
#> 32 32          32       0
#> 33 33          33       1
#> 34 34          34       0
#> 35 35          35       0
#> 36 36          36       2
#> 37 37          37       0
#> 38 38          38       2
#> 39 39          39       2
#> 40 40          40       0
#> 41 41          41       1
#> 42 42          42       0
#> 43 43          43       0
#> 44 44          44       0

Created on 2019-03-21 by the reprex package (v0.2.1)

1 Like

+1, using %in% is the right move.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.