0

I would like to add a factor N/P to the following data frame:

   time   subjects  value
1     0     sub1    10
2     1     sub1    11
3     2     sub1    12
...
10    9     sub1    19
11   10     sub1    20
12    0     sub2    20
13    1     sub2    21
...
21    9     sub2    29
22   10     sub2    30
23    0     sub3    30
24    1     sub3    31
25    2     sub3    32
...
33   10     sub3    40

so that it reads as follows

   time   subjects  value  status
1     0     sub1    10     N
2     1     sub1    11     N
3     2     sub1    12     N
...
10    9     sub1    19     N
11   10     sub1    20     N
12    0     sub2    20     P
13    1     sub2    21     P
...
21    9     sub2    29     P
22   10     sub2    30     P
23    0     sub3    30     N
24    1     sub3    31     N
25    2     sub3    32     N
...
33   10     sub3    40     N

The 'status' factors are stored so far in an array c("N","P","N",....,"P","P") for hundreds of subjects.

markus
  • 25,843
  • 5
  • 39
  • 58
mjs
  • 150
  • 1
  • 20

1 Answers1

1

If your array has the same amount of elements as there are observations in the data.frame, then you can simply do

df$status <- status_array

Of course, that is if they are ordered the same.

DevGin
  • 443
  • 3
  • 12
  • Unfortunately, the array length is equal the numer of subjects. The data frame contains 200 observations for each of the 100 or so subjects. It would mean to modify the array like that 'statusArray <- c(rep("N",200),... ,rep("P",200),rep("P",200))', but I don't want to do it by hand, any idea how to do it smarter? – mjs Oct 05 '18 at 20:25
  • 2
    @maciekj I guess you would need to do `rep(statusArray, each = 200)` – markus Oct 05 '18 at 20:28
  • 1
    Your question seems vague. Is the "N" or "P" value based on certain conditions? I don't think you gave enough information to get a good response. – DevGin Oct 05 '18 at 20:29
  • @markus idea was exactly what I needed! – mjs Oct 05 '18 at 20:31
  • @MarkGingrass there is not rule how the N/P are assigned, it's a result of a simulation experiment. – mjs Oct 05 '18 at 20:32