R programming How to subset data and plot graphs in R

+1 vote

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, 2019 in Data Analytics by Sophie may
• 10,620 points
1,001 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, 2019 by Tyrion anex
• 8,700 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 17, 2018 in Data Analytics by DataKing99
• 8,250 points
1,920 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 19, 2018 in Data Analytics by DataKing99
• 8,250 points
2,198 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 Sahiti
• 6,370 points
1,908 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 Abhi
• 3,720 points
952 views
+10 votes
3 answers

Which is a better initiative to learn data science: Python or R?

Well it truly depends on your requirement, If ...READ MORE

answered Aug 9, 2018 in Data Analytics by Abhi
• 3,720 points
1,669 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,840 points
767 views
0 votes
1 answer
+1 vote
3 answers

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$sym ...READ MORE

answered Jan 11, 2019 in Data Analytics by Tyrion anex
• 8,700 points
35,868 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,700 points
1,348 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP