Re: [R] Replicating Rows
Hi, apple- read.table(text= Fam.name,Item,AMT.SALE.NET.PROMO,X.CY..QTY.SALE.TOT 9475,Imported Fruits,22110276001,0,436 9499,Imported Fruits,22110277001,0,236 9523,Imported Fruits,22110278001,0,71 ,sep=,,header=TRUE,stringsAsFactors=FALSE) str(apple) #'data.frame': 3 obs. of 4 variables: # $ Fam.name : chr Imported Fruits Imported Fruits Imported Fruits # $ Item : num 2.21e+10 2.21e+10 2.21e+10 # $ AMT.SALE.NET.PROMO: int 0 0 0 # $ X.CY..QTY.SALE.TOT: num 436 236 71 Here, it changed the class of some of the variables. new-sapply(apple[,-4],rep,apple[,4]) str(as.data.frame(new,stringsAsFactors=FALSE)) #'data.frame': 743 obs. of 3 variables: # $ Fam.name : chr Imported Fruits Imported Fruits Imported Fruits Imported Fruits ... # $ Item : chr 22110276001 22110276001 22110276001 22110276001 ... # $ AMT.SALE.NET.PROMO: chr 0 0 0 0 ... new1-apple[rep(seq_len(nrow(apple)),apple[,4]),-4] row.names(new1)- 1:nrow(new1) str(new1) #'data.frame': 743 obs. of 3 variables: # $ Fam.name : chr Imported Fruits Imported Fruits Imported Fruits Imported Fruits ... # $ Item : num 2.21e+10 2.21e+10 2.21e+10 2.21e+10 2.21e+10 ... # $ AMT.SALE.NET.PROMO: int 0 0 0 0 0 0 0 0 0 0 .. A.K. I try to replicate the rows according to the number of quantity occurred. Its row should be be sum of the quantity. is there any wrong with my code? thanks. apple Fam.name Item AMT.SALE.NET.PROMO X.CY..QTY.SALE.TOT 9475 Imported Fruits 22110276001 0 436 9499 Imported Fruits 22110277001 0 236 9523 Imported Fruits 22110278001 0 71 9552 Imported Fruits 22110306001 0 69 9571 Imported Fruits 22110314001 0 20 9579 Imported Fruits 22110315001 0 80 9604 Imported Fruits 22110317001 0 61 9635 Imported Fruits 22110321001 0 1026 9697 Imported Fruits 22110334001 0 223 9720 Imported Fruits 22110335001 0 214 9744 Imported Fruits 22110336001 0 102 9768 Imported Fruits 22110337001 0 146 9868 Imported Fruits 22110354001 118.8 17 9893 Imported Fruits 22110360001 0 43 9904 Imported Fruits 22110363001 0 49 9920 Imported Fruits 22110364001 0 1 9938 Imported Fruits 22110365001 205.4 33 new-sapply(apple[,-4],rep,apple[,4]) nrow(new) [1] 33572 __ R-help@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.
Re: [R] Replicating Rows
Another way is using straightforward indexing: x - cbind(trips=c(1,3,2), y=1:3, z=4:6) x trips y z [1,] 1 1 4 [2,] 3 2 5 [3,] 2 3 6 # generate row indices with the appropriate # number of repeats ii - rep(seq(len=nrow(x)), x[,1]) [1] 1 2 2 2 3 3 # use these indices to select data rows x[ii, -1] y z [1,] 1 4 [2,] 2 5 [3,] 2 5 [4,] 2 5 [5,] 3 6 [6,] 3 6 Jorge Ivan Velez wrote: Hi Marion, Try this: set.seed(123) mydf=data.frame(trips=rpois(10,5), matrix(rnorm(10*5),ncol=5)) mydf sapply(mydf[,-1],rep,mydf[,1]) HTH, Jorge On Wed, May 7, 2008 at 11:41 PM, [EMAIL PROTECTED] wrote: Hi, I have a data matrix in which there are 1000 rows by 30 columns. The first column of each row is a numeric indicating the number of trips taken to a particular location with location attributes in the following column entries for that row. I want to repeat each row based on the number of trips taken (as indicated by the number in the first column)...i.e., if 1,1 indicates 4 trips, I want to replicate row 1 four times, and do this for each entry of column 1. I have played with rep command with little luck. Can anyone help? This is probably very simple. Thank you, mw Marion Wittmann, Ph.D. candidate Environmental Science and Management University of California Santa Barbara, CA 93106-5131 __ R-help@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. [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
[R] Replicating Rows
Hi, I have a data matrix in which there are 1000 rows by 30 columns. The first column of each row is a numeric indicating the number of trips taken to a particular location with location attributes in the following column entries for that row. I want to repeat each row based on the number of trips taken (as indicated by the number in the first column)...i.e., if 1,1 indicates 4 trips, I want to replicate row 1 four times, and do this for each entry of column 1. I have played with rep command with little luck. Can anyone help? This is probably very simple. Thank you, mw Marion Wittmann, Ph.D. candidate Environmental Science and Management University of California Santa Barbara, CA 93106-5131 __ R-help@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.
Re: [R] Replicating Rows
Hi Marion, Try this: set.seed(123) mydf=data.frame(trips=rpois(10,5), matrix(rnorm(10*5),ncol=5)) mydf sapply(mydf[,-1],rep,mydf[,1]) HTH, Jorge On Wed, May 7, 2008 at 11:41 PM, [EMAIL PROTECTED] wrote: Hi, I have a data matrix in which there are 1000 rows by 30 columns. The first column of each row is a numeric indicating the number of trips taken to a particular location with location attributes in the following column entries for that row. I want to repeat each row based on the number of trips taken (as indicated by the number in the first column)...i.e., if 1,1 indicates 4 trips, I want to replicate row 1 four times, and do this for each entry of column 1. I have played with rep command with little luck. Can anyone help? This is probably very simple. Thank you, mw Marion Wittmann, Ph.D. candidate Environmental Science and Management University of California Santa Barbara, CA 93106-5131 __ R-help@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. [[alternative HTML version deleted]] __ R-help@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.