1

I have some within Subject measures and I want to assign a new Group variable to Subject Numbers (e.g: Subjects from 1 to 15 should be labeled as a Control Group and 15 to 30 as an Experimental Group).

I have 36 between subject trial measures for each Subject. I would like to create a new column where each subject trial will be labeled as Exp. or Contr. trial.

      Subject Condition
1         10      Cont
2         10      Cont
3         10      Cont
4         10      Cont
5         10      Cont
6         10      Cont
7         10      Cont
8         10      Cont
9         10      Cont
10        10       Exp
11        10      Cont
12        10      Cont
13        10      Cont
14        10      Cont
15        10      Cont
16        10      Cont
17        10      Cont
18        10      Cont
19        10      Cont
20        10      Cont
21        10      Cont
22        10      Cont
23        10       Exp
24        10      Cont
25        10      Cont
26        10      Cont
27        10      Cont
28        10      Cont
29        10      Cont
30        10      Cont
31        10      Cont
32        10      Cont
33        10      Cont
34        10      Cont
35        10      Cont
36        10       Exp
37        11       Exp
38        11      Cont
39        11      Cont
40        11      Cont
41        11      Cont
42        11      Cont
43        11      Cont
44        11      Cont
45        11      Cont
46        11      Cont
47        11      Cont
48        11      Cont
49        11      Cont
50        11       Exp
51        11      Cont
52        11      Cont
53        11      Cont
54        11      Cont
55        11      Cont
56        11      Cont
57        11      Cont
58        11      Cont
59        11      Cont
60        11      Cont
61        11      Cont
62        11      Cont
63        11       Exp
64        11      Cont
65        11      Cont
66        11      Cont
67        11      Cont
68        11      Cont
69        11      Cont
70        11      Cont
71        11      Cont
72        11      Cont
73        12      Cont
74        12      Cont
75        12      Cont
76        12      Cont
77        12       Exp
78        12      Cont
79        12      Cont
80        12      Cont
81        12      Cont
82        12      Cont
83        12      Cont
84        12      Cont
85        12      Cont
86        12      Cont
87        12      Cont
88        12      Cont
89        12      Cont
90        12       Exp
91        12      Cont
92        12      Cont
93        12      Cont
94        12      Cont
95        12      Cont
96        12      Cont
97        12      Cont
98        12      Cont
99        12      Cont
100       12      Cont
101       12      Cont
102       12      Cont
103       12       Exp
104       12      Cont
105       12      Cont
106       12      Cont
107       12      Cont
108       12      Cont
109       13      Cont
110       13      Cont
111       13      Cont
112       13      Cont
          ...

I used (as suggsted by Craig):

  mutate(Condition = ifelse(Subject == 1:15, "Exp", "Cont"))

But I get mixed results..

Tamara Keynes
  • 21
  • 1
  • 5
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Pictures of data are not helpful because we cannot copy/paste the values. – MrFlick Oct 16 '19 at 15:41
  • Thank you for your reply! I edited the question.. – Tamara Keynes Oct 16 '19 at 16:15
  • 1
    Please don't post the same question twice. How does this differ from your other post today? – camille Oct 16 '19 at 19:11

2 Answers2

0

It's hard to know for sure what you want if you don't have a reproducible example of your data. But, if I understand your code correctly then your syntax is a little off for mutate. Here's the general flow of code for an ifelse statement:

ifelse(logical test, output if true, output if false)

So, if you only have 30 subjects then you don't need to specify subjects 16:30 in the check since they are the "else" outcome.

res %>%
  mutate(Condition = ifelse(Subject %in% 1:15, "Exp", "Cont"))
  • You don't need to put your new variable in quotations
  • You do need %in% to indicate a check is occurring
Trent
  • 771
  • 5
  • 19
0

You have asked the same question on SO already. Try the following approach:

mutate(Condition = ifelse(between(Subject,1,15), "Exp", "Cont"))
Anisha Singhal
  • 115
  • 1
  • 7