Hi, Ia m working in discretized data. Here my data: x <- c(2,1,3, 5), and I want to make (0,1) data based on the length of each component in x. So the new data should like: y = (0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1). I spent too much time with "seq", "rep". Still didn't get it. Any help? Thanks Ilham [[alternative HTML version deleted]]
Hi The "trick" is to define a function f() that does what you want elementwise, then use lapply(): > f <- function(i){c(rep(0,i-1),1)} > x <- c(2,1,3,5) > c(lapply(x,f),recursive=T) [1] 0 1 1 0 0 1 0 0 0 0 1 > HTH rksh> Hi, Ia m working in discretized data. Here my data: > > x <- c(2,1,3, 5), and I want to make (0,1) data based on the > length of > each component in x. > So the new data should like: y = (0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1). > I spent > too much time with > "seq", "rep". Still didn't get it. Any help? Thanks > > Ilham > > [[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.-- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
sapply(x,FUN=function(y) {c(rep(0,y-1),1)}) On 16 Nov 2007, at 10:36, G Ilhamto wrote:> Hi, Ia m working in discretized data. Here my data: > > x <- c(2,1,3, 5), and I want to make (0,1) data based on the > length of > each component in x. > So the new data should like: y = (0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1). > I spent > too much time with > "seq", "rep". Still didn't get it. Any help? Thanks > > Ilham > > [[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.Ingmar Visser Department of Psychology, University of Amsterdam Roetersstraat 15 1018 WB Amsterdam The Netherlands t: +31-20-5256723 [[alternative HTML version deleted]]