r - Create new variable (with values rep(c(1,2) depending on repeating values in other variable -


i relatively new r , working on following problem:

i have data frame set more or less structured follows:

    filename                          f_w07fp_w10hs_talkturns_i1.docx        0     f_w07fp_w10hs_talkturns_i1.docx        1     f_w07fp_w10hs_talkturns_i1.docx        2     f_s02be_g04kh_talkturnsi1.docx         1     f_s02be_g04kh_talkturnsi1.docx         1     f_s02be_g04kh_talkturnsi1.docx         1     f_s02be_g04kh_talkturnsi1.docx         2     f_s02be_g04kh_talkturnsi1.docx         3     f_l05rj_l11sm_talk turnsi1.docx        5      f_l05rj_l11sm_talk turnsi1.docx        2     f_l05rj_l11sm_talk turnsi1.docx        1 

now want add new column repeating values c(1,2) works fine if this:

    data_rlsm[,length(data_rlsm[1,])+1] <-rep(c(1,2))  

but in final data set need values 1,2 start again depending on values in first line. need r restart counting 1 when values in first column change. filename first column whereas speaker added behind last column. in end should this:

    filename                               x   speaker     f_w07fp_w10hs_talkturns_i1.docx        0   1      f_w07fp_w10hs_talkturns_i1.docx        1   2      f_w07fp_w10hs_talkturns_i1.docx        2   1      f_s02be_g04kh_talkturnsi1.docx         1   1     f_s02be_g04kh_talkturnsi1.docx         1   2     f_s02be_g04kh_talkturnsi1.docx         1   1     f_s02be_g04kh_talkturnsi1.docx         2   2      f_s02be_g04kh_talkturnsi1.docx         3   1     f_l05rj_l11sm_talk turnsi1.docx        5   1     f_l05rj_l11sm_talk turnsi1.docx        2   2      f_l05rj_l11sm_talk turnsi1.docx        1   1 

i've been trying this:

for(i in 1:name)  { data_rlsm[length(data_rlsm[1,])+1] <-rep_len(c(1,2), length.out = length(unique(data_rlsm[,i]))  } 

but honest, have absolutely no idea doing. appreciate ideas or ways tackle problem :)

thank you!

let's start dataset:

df <- data.frame(filename = c(rep('a', 3), rep('b', 4), rep('c', 2))) 

and then:

require(dplyr) df <- df %>%   group_by(., filename) %>%   mutate(., speaker = rep(c(1, 2), length.out = n())) 

and done!


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -