I know you can use in R
tail()
and
head()
to count when a specific value is different from the previous one, but can I do this for multiple columns? I need to make a column that counts 1 if the current row (LAST 4 COLUMNS ONLY) is different than the previous one.
here is an example of my data:
> Standing Stepping Cycling New_Sitting
1 0 0 0 1
2 0 0 0 1
3 0 0 0 1
4 0 0 0 1
5 0 0 0 1
6 0 0 0 1
7 0 0 0 1
8 0 0 0 1
9 0 0 0 1
10 0 0 0 1
11 0 0 0 1
12 0 0 0 1
13 0 0 0 1
14 0 0 0 1
15 0 0 0 1
16 0 0 0 1
17 0 0 0 1
18 0 0 0 1
19 0 0 0 1
20 0 0 0 1
21 0 0 0 1
22 0 0 0 1
23 0 0 0 1
24 0 0 0 1
25 0 0 0 1
26 0 0 0 1
27 0 0 0 1
28 0 0 0 1
29 0 0 0 1
30 0 0 0 1
31 0 0 0 1
32 0 0 0 1
33 0 0 0 1
34 0 0 0 1
35 0 0 0 1
36 0 0 0 1
37 0 0 0 1
38 0 0 0 1
39 0 0 0 1
40 0 0 0 1
41 0 0 0 1
42 0 0 0 1
43 0 0 0 1
44 0 0 0 1
45 0 0 0 1
46 0 0 0 1
47 0 0 0 1
48 0 0 0 1
49 0 0 1 0
50 0 1 0 0
So basically, the last 4 columns represent the posture of the person, I need to have a column that counts (named counter, or anything else logical) that is 1 when they change position, 0 when they don't. Let me know if you need more information.
In this case the answer should be:
count = 2