Hi, there, I have a simple data manipulation question for you. Thank you for your help! Suppose that I have this data about people appearing in a class Mary Mary Mary Sam Sam John John John John Then I want to find out what exact time(s) the student appears at the moment such as Mary 1 Mary 2 Mary 3 Sam 1 Sam 2 John 1 John 2 John 3 John 4 the fifth row shows tha Sam show the second times at the that moment. How can I manipulate the data in this way. Suppose that now I just have "name" variable and want to add a colume of frequency? Best Jie [[alternative HTML version deleted]]
one way is with ave(), e.g., dat <- data.frame(name = rep(c("Mary", "Sam", "John"), c(3,2,4))) dat$freq <- ave(seq_along(dat$name), dat$name, FUN = seq_along) dat I hope it helps. Best, Dimitris jie feng wrote:> Hi, there, > > I have a simple data manipulation question for you. Thank you for your help! > > Suppose that I have this data about people appearing in a class > > > Mary > Mary > Mary > Sam > Sam > John > John > John > John > > Then I want to find out what exact time(s) the student appears at the > moment such as > > Mary 1 > Mary 2 > Mary 3 > Sam 1 > Sam 2 > John 1 > John 2 > John 3 > John 4 > > the fifth row shows tha Sam show the second times at the that moment. > > How can I manipulate the data in this way. Suppose that now I just have > "name" variable and want to add a colume of frequency? > > Best > > Jie > > [[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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
See ?ave and ?seq_along DF <- data.frame(Name = c("Mary", "Mary", "Mary", "Sam", "Sam", "John", "John", "John", "John"), stringsAsFactors = FALSE) DF$index <- ave(1:nrow(DF), DF$Name, FUN = seq_along) On Sat, Nov 8, 2008 at 5:43 AM, jie feng <jiefeng222 at gmail.com> wrote:> Hi, there, > > I have a simple data manipulation question for you. Thank you for your help! > > Suppose that I have this data about people appearing in a class > > > Mary > Mary > Mary > Sam > Sam > John > John > John > John > > Then I want to find out what exact time(s) the student appears at the > moment such as > > Mary 1 > Mary 2 > Mary 3 > Sam 1 > Sam 2 > John 1 > John 2 > John 3 > John 4 > > the fifth row shows tha Sam show the second times at the that moment. > > How can I manipulate the data in this way. Suppose that now I just have > "name" variable and want to add a colume of frequency? > > Best > > Jie > > [[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. >