i have a dataset which consists of different measurements in each column and the last column consists of values (0,1,2).
for example lets say my dataframe looks like this (ignore the values of v1:v5)
1. v1 v2 v3 v4 v5 v6
2. 24 76 98 89 87 2
3. 24 76 98 89 87 2
4. 24 76 98 89 87 1
5. 24 76 98 89 87 2
6. 24 76 98 89 87 2
I am interested in the values of v6 column and i want to extract the rows where the value equals to 2. In the above example, i would like to extract the first 2 rows and save them as a new dataframe and also extract the 5th and 6th row as a different dataframe and save this too. To be more clear, when my values are equal to 2 and are consequtive, i need them saved as a new dataframe. when the value is different, i need the loop to ignore it and find again the interested value (which is 2). If my dataframe has 70 blocks of consequtive 2 in the last column i need to end up with 70 dataframes.
I tried for loop but i am fairly new to R and programming and i am stuck.
this is what i tried so far:
>
>
>
>x=1
>for (i in 1:nrow(dataframe)) {
>
> if (dataframe[i,lastcolumn] == 2 && x==1) {
>
> start.event <- dataframe[i,]
>
> }
>
> if (dataframe[i,lastcolumn] != 2) {
> end.event <- dataframe[i-1,]
>
> }
>
> else {
>
> df[1] <- dataframe( start.event:end.event , )
> x = 1
> }
> }
I would really appreciate any help.
Thanks in advance
data.framebe two rows, or should the program grab all consecutive rows ?which( diff(as.integer(rownames(dataframe[dataframe$V6 == 2,])) ) == 1)