R programming: How to subset data and plot graphs in R?

0 votes

I have a data set consisting of 0s and 1s in a column. I figured out the row index where the toggling happens and now I want to sample out data from these by setting these particular row IDS. 

Here's my data frame:

row id   mode 
1          0
2          0
3          1
4          1
5          0
6          0
7          0
8          1
9          1
10         1

Once I split the data frame, I should get 4 other data frames like so:

y[1] : 
row id   mode 
1           0
2           0

y[2]
row id     mode 
3            1 
4            1

y[3]
row id      mode 
5            0
6            0
7            0

Feb 18 in Data Analytics by Sophie may
• 9,430 points
46 views

1 answer to this question.

0 votes

You can create a grouping variable depending on the difference of adjacent elements in 'mode' and then split the data set:

split(df1, cumsum(c(TRUE, diff(df1$mode)!=0)))
#$`1`
#  row id mode
#1      1    0
#2      2    0

#$`2`
#  row id mode
#3      3    1
#4      4    1

#$`3`
#  row id mode
#5      5    0
#6      6    0
#7      7    0

#$`4`
#   row id mode
#8       8    1
#9       9    1
#10     10    1
answered Feb 18 by Tyrion anex
• 8,280 points

Related Questions In Data Analytics

0 votes
1 answer

How to filter a data frame with dplyr and tidy evaluation in R?

Requires the use of map_df to run each model, ...READ MORE

answered May 16, 2018 in Data Analytics by DataKing99
• 8,100 points
61 views
0 votes
1 answer

How to forecast season and trend of data using STL and ARIMA in R?

You can use the forecast.stl function for the ...READ MORE

answered May 18, 2018 in Data Analytics by DataKing99
• 8,100 points
453 views
0 votes
1 answer

How to calculate group mean and assign it to new data in R

You can use something like this: df$grp.mean.values <- ...READ MORE

answered Jun 27, 2018 in Data Analytics by darklord
• 6,140 points
40 views
0 votes
1 answer

Which package is used to do data import in R and Python and How do you import SAS data?

We can do data import using multiple ...READ MORE

answered Aug 24, 2018 in Data Analytics by ANMOL
• 3,620 points
20 views
+1 vote
2 answers
0 votes
1 answer

R programming logic

Use gsub to match the substring that we want ...READ MORE

answered Nov 16, 2018 in Data Analytics by Maverick
• 10,040 points
21 views
0 votes
1 answer
+4 votes
2 answers
0 votes
1 answer

How to change the value of a variable using R programming in a data frame?

Try this: df$symbol <- as.character(df$symbol) df$symbol[df$symb ...READ MORE

answered Jan 11 in Data Analytics by Tyrion anex
• 8,280 points
80 views
0 votes
1 answer

R programming: How to pass variables from a r program to mysql function?

To include the R variables called start.date and end.date, you can use paste to ...READ MORE

answered Dec 28, 2018 in Data Analytics by Tyrion anex
• 8,280 points
57 views