Dear R-helpers, i have a question on how to vectorize this problem: i have a dataframe: tester <- data.frame(groups=c("A","A","B","B","C","C"), one=c(1,1,2,2,3,3), two=c(6,6,7,7,8,8)) # i split it into a list tester.L <- split(tester, tester$groups) # And want to keep only the first item in each: lapply(tester.L, function(x) x <- x[1,] ) How do i make a dataframe out of the last result, which looks like "tester", without looping? (i can use rbind in a for loop, but is rather slow) thanks for your help, Remko Duursma ____________________________________________________________ Get advanced SPAM filtering on Webmail or POP Mail ... Get Lycos Mail!
> tester <- data.frame(groups=c("A","A","B","B","C","C"),one=c(1,1,2,2,3,3), two=c(6,6,7,7,8,8)) > tester.L <- split(tester, tester$groups) > as.data.frame(lapply(tester.L, function(x) x <- unlist(x[1,] ))) A B C groups 1 2 3 one 1 2 3 two 6 7 8 How's this? Spencer Graves Remko Duursma wrote:> Dear R-helpers, > > i have a question on how to vectorize this problem: > > i have a dataframe: > > tester <- data.frame(groups=c("A","A","B","B","C","C"), one=c(1,1,2,2,3,3), two=c(6,6,7,7,8,8)) > > # i split it into a list > tester.L <- split(tester, tester$groups) > > # And want to keep only the first item in each: > lapply(tester.L, function(x) x <- x[1,] ) > > > How do i make a dataframe out of the last result, which looks like "tester", without looping? (i can use rbind in a for loop, but is rather slow) > > thanks for your help, > > Remko Duursma > > > ____________________________________________________________ > Get advanced SPAM filtering on Webmail or POP Mail ... Get Lycos Mail! > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
You could try: s <- lapply(tester.L, function(x) x <- x[1,] ) do.call("rbind", s) -roger _______________________________ UCLA Department of Statistics http://www.stat.ucla.edu/~rpeng On Tue, 15 Apr 2003, Remko Duursma wrote:> Dear R-helpers, > > i have a question on how to vectorize this problem: > > i have a dataframe: > > tester <- data.frame(groups=c("A","A","B","B","C","C"), one=c(1,1,2,2,3,3), two=c(6,6,7,7,8,8)) > > # i split it into a list > tester.L <- split(tester, tester$groups) > > # And want to keep only the first item in each: > lapply(tester.L, function(x) x <- x[1,] ) > > > How do i make a dataframe out of the last result, which looks like > "tester", without looping? (i can use rbind in a for loop, but is > rather slow) > > thanks for your help, > > Remko Duursma > > > ____________________________________________________________ > Get advanced SPAM filtering on Webmail or POP Mail ... Get Lycos Mail! > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
Remko Duursma wrote:> Dear R-helpers, > > i have a question on how to vectorize this problem: > > i have a dataframe: > > tester <- data.frame(groups=c("A","A","B","B","C","C"), one=c(1,1,2,2,3,3), two=c(6,6,7,7,8,8)) > > # i split it into a list > tester.L <- split(tester, tester$groups) > > # And want to keep only the first item in each: > lapply(tester.L, function(x) x <- x[1,] ) > > > How do i make a dataframe out of the last result, which looks like "tester", without looping? (i can use rbind in a for loop, but is rather slow) >If you're always trying to get the unique rows, then just use unique(): > tester groups one two 1 A 1 6 2 A 1 6 3 B 2 7 4 B 2 7 5 C 3 8 6 C 3 8 > unique(tester) groups one two 1 A 1 6 3 B 2 7 5 C 3 8 Or use do.call("rbind", ...) > do.call("rbind", lapply(split(tester, tester$group), + function(x) x[1, ])) groups one two A 1 1 6 B 2 2 7 C 3 3 8 > Or just for fun: > tester[which(!duplicated(tester$groups)), ] groups one two 1 A 1 6 3 B 2 7 5 C 3 8 Regards, Sundar
you can try this,... data.frame(t(sapply(tester.L, function(x) x <- x[1,] ))) Mahbub. --- Remko Duursma <den.duurs at lycos.com> wrote:> Dear R-helpers, > > i have a question on how to vectorize this problem: > > i have a dataframe: > > tester <- > data.frame(groups=c("A","A","B","B","C","C"), > one=c(1,1,2,2,3,3), two=c(6,6,7,7,8,8)) > > # i split it into a list > tester.L <- split(tester, tester$groups) > > # And want to keep only the first item in each: > lapply(tester.L, function(x) x <- x[1,] ) > > > How do i make a dataframe out of the last result, > which looks like "tester", without looping? (i can > use rbind in a for loop, but is rather slow) > > thanks for your help, > > Remko Duursma > > >____________________________________________________________> Get advanced SPAM filtering on Webmail or POP Mail > ... Get Lycos Mail! > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help __________________________________________________ The New Yahoo! Search - Faster. Easier. Bingo
Mahbub's solution data.frame(t(sapply(...))) is much faster than do.call("rbind", lapply(...)) as tested on a larger dataset. Thanks everyone, Remko Duursma -- On Tue, 15 Apr 2003 22:10:28 Mahbub Latif wrote:>you can try this,... >data.frame(t(sapply(tester.L, function(x) x <- x[1,] >))) > >Mahbub. > >--- Remko Duursma <den.duurs at lycos.com> wrote: >> Dear R-helpers, >> >> i have a question on how to vectorize this problem: >> >> i have a dataframe: >> >> tester <- >> data.frame(groups=c("A","A","B","B","C","C"), >> one=c(1,1,2,2,3,3), two=c(6,6,7,7,8,8)) >> >> # i split it into a list >> tester.L <- split(tester, tester$groups) >> >> # And want to keep only the first item in each: >> lapply(tester.L, function(x) x <- x[1,] ) >> >> >> How do i make a dataframe out of the last result, >> which looks like "tester", without looping? (i can >> use rbind in a for loop, but is rather slow) >> >> thanks for your help, >> >> Remko Duursma >> >> >> >____________________________________________________________ >> Get advanced SPAM filtering on Webmail or POP Mail >> ... Get Lycos Mail! >> >> ______________________________________________ >> R-help at stat.math.ethz.ch mailing list >> >https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > >__________________________________________________ >Do you Yahoo!?>http://search.yahoo.com >