Igor Mintz
2013-Apr-18  09:33 UTC
[R] dividing a long column to many short ones by a condition
hello i have a very long column of numbers. i want R to make a new column every time the value changes from zero. example for the column: 90.1194354 87.94788274 80.34744843 64.06080347 30.40173724 0 0 0 0 0 16.28664495 23.88707926 29.31596091 48.85993485 13.02931596 0 0 0 7.600434311 20.62975027 29.31596091 32.5732899 for this example i want to get 3 columns. thanks! [[alternative HTML version deleted]]
andrija djurovic
2013-Apr-18  11:18 UTC
[R] dividing a long column to many short ones by a condition
Hi Igor.
Here is one way:
DF <- read.table(textConnection("90.1194354
87.94788274
80.34744843
64.06080347
30.40173724
0
0
0
0
0
16.28664495
23.88707926
29.31596091
48.85993485
13.02931596
0
0
0
7.600434311
20.62975027
29.31596091
32.5732899"), header=FALSE)
a <- DF$V1[which(DF$V1!=0)]
indx <- which(DF$V1!=0)
blocks <- cut(1:length(a), breaks=c(0,which(diff(indx)!=1), length(a)))
n_levels <- length(levels(blocks))
l <- vector("list", n_levels)
for(i in 1:n_levels)
{
l[[i]] <- a[blocks==levels(blocks)[i]]
}
l
Andrija
On Thu, Apr 18, 2013 at 11:33 AM, Igor Mintz <igormintz@gmail.com> wrote:
> hello
> i have a very long column of numbers. i want R to make a new column every
> time the value changes from zero.
> example for the column:
> 90.1194354
> 87.94788274
> 80.34744843
> 64.06080347
> 30.40173724
> 0
> 0
> 0
> 0
> 0
> 16.28664495
> 23.88707926
> 29.31596091
> 48.85993485
> 13.02931596
> 0
> 0
> 0
> 7.600434311
> 20.62975027
> 29.31596091
> 32.5732899
>
> for this example i want to get 3 columns.
>
> thanks!
>
>         [[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.
>
	[[alternative HTML version deleted]]
Rui Barradas
2013-Apr-18  11:24 UTC
[R] dividing a long column to many short ones by a condition
Hello, Something like this? grp <- cumsum(abs(c(0, diff(x == 0)))) tmp <- lapply(split(x, grp), function(x) if(all(x == 0)) NULL else x) tmp[sapply(tmp, function(x) !is.null(x))] Hope this helps, Rui Barradas Em 18-04-2013 10:33, Igor Mintz escreveu:> hello > i have a very long column of numbers. i want R to make a new column every > time the value changes from zero. > example for the column: > 90.1194354 > 87.94788274 > 80.34744843 > 64.06080347 > 30.40173724 > 0 > 0 > 0 > 0 > 0 > 16.28664495 > 23.88707926 > 29.31596091 > 48.85993485 > 13.02931596 > 0 > 0 > 0 > 7.600434311 > 20.62975027 > 29.31596091 > 32.5732899 > > for this example i want to get 3 columns. > > 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. >
Rui Barradas
2013-Apr-18  11:31 UTC
[R] dividing a long column to many short ones by a condition
Hello, Sorry, forgot the instruction to read in the data. Now the complete code. x <- scan(text = " 90.1194354 87.94788274 80.34744843 64.06080347 30.40173724 0 0 0 0 0 16.28664495 23.88707926 29.31596091 48.85993485 13.02931596 0 0 0 7.600434311 20.62975027 29.31596091 32.5732899 ") x grp <- cumsum(abs(c(0, diff(x == 0)))) tmp <- lapply(split(x, grp), function(x) if(all(x == 0)) NULL else x) tmp[sapply(tmp, function(x) !is.null(x))] Rui Barradas Em 18-04-2013 12:24, Rui Barradas escreveu:> Hello, > > Something like this? > > > grp <- cumsum(abs(c(0, diff(x == 0)))) > tmp <- lapply(split(x, grp), function(x) if(all(x == 0)) NULL else x) > tmp[sapply(tmp, function(x) !is.null(x))] > > > Hope this helps, > > Rui Barradas > > Em 18-04-2013 10:33, Igor Mintz escreveu: >> hello >> i have a very long column of numbers. i want R to make a new column every >> time the value changes from zero. >> example for the column: >> 90.1194354 >> 87.94788274 >> 80.34744843 >> 64.06080347 >> 30.40173724 >> 0 >> 0 >> 0 >> 0 >> 0 >> 16.28664495 >> 23.88707926 >> 29.31596091 >> 48.85993485 >> 13.02931596 >> 0 >> 0 >> 0 >> 7.600434311 >> 20.62975027 >> 29.31596091 >> 32.5732899 >> >> for this example i want to get 3 columns. >> >> 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. >> > > ______________________________________________ > 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.