srecko joksimovic
2013-Aug-27 19:29 UTC
[R] Iterate over rows and update values based on condition
Hi, I have a data set with structure similar to this: id user action 1 12 login 2 12 view 3 12 view 4 12 view 5 12 login 6 12 view 7 12 view 8 12 login I want to create a list of sessions. That means to split table on every occurrence of "login". Using Java (or some other language), I would probably iterate through rows and create new List instance on every "login", but I guess there is more efficient way to do that using R? Thanks [[alternative HTML version deleted]]
Hi, May be this helps: dat1<- read.table(text=" id??? user??? action 1????? 12????? login 2????? 12????? view 3????? 12????? view 4????? 12????? view 5????? 12????? login 6????? 12????? view 7????? 12????? view 8????? 12????? login ",sep="",header=TRUE,stringsAsFactors=FALSE) split(dat1,cumsum(dat1$action=="login")) #$`1` ?# id user action #1? 1?? 12? login #2? 2?? 12?? view #3? 3?? 12?? view #4? 4?? 12?? view # #$`2` ?# id user action #5? 5?? 12? login #6? 6?? 12?? view #7? 7?? 12?? view # #$`3` ?# id user action #8? 8?? 12? login A.K. ----- Original Message ----- From: srecko joksimovic <sreckojoksimovic at gmail.com> To: R-help at r-project.org Cc: Sent: Tuesday, August 27, 2013 3:29 PM Subject: [R] Iterate over rows and update values based on condition Hi, I have a data set with structure similar to this: id? ? user? ? action 1? ? ? 12? ? ? login 2? ? ? 12? ? ? view 3? ? ? 12? ? ? view 4? ? ? 12? ? ? view 5? ? ? 12? ? ? login 6? ? ? 12? ? ? view 7? ? ? 12? ? ? view 8? ? ? 12? ? ? login I want to create a list of sessions. That means to split table on every occurrence of "login". Using Java (or some other language), I would probably iterate through rows and create new List instance on every "login", but I guess there is more efficient way to do that using R? Thanks ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.