Reputation: 171
I have a "csv" file which includes 3 columns, 100+rows. The variables in all columns change according to the data placed in Column 1, the "Time".
Time Temp Cloud
1100 22 1
1102 14 1
1104 14 2
1106 23 1
1108 12 1
1110 21 2
1112 17 2
1114 12 3
1116 24 3
I want to know when "Cloud" changes [ e.g. at 3rd and 6th row ], and I want to obtain the other variables which is placed at that row, and the row before that row. How can I do that ? Thanks
Upvotes: 0
Views: 78
Reputation: 2651
I would do something like this:
df$Change <- c(0,sign(diff(df$Cloud)))
subset(df,Change!=0)[,4]
This will eliminate rows where there are no changes.
Upvotes: 1
Reputation: 42629
diff
will almost do this directly. Apply it twice. Calling your example data d
:
> d[c(diff(d$Cloud) != 0,FALSE) | c(FALSE, diff(d$Cloud) != 0),]
Time Temp Cloud
2 1102 14 1
3 1104 14 2
4 1106 23 1
5 1108 12 1
6 1110 21 2
7 1112 17 2
8 1114 12 3
Upvotes: 1