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
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 -
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
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 -
Hi.
I completely forgot split function so loop:
l - vector(list, n_levels)
for(i in 1:n_levels)
{
l[[i]] - a[blocks==levels(blocks)[i]]
}
l
could be substitute with:
split(a, blocks),
but anyway Rui's solution is better.
Andrija
On Thu, Apr 18, 2013 at 1:18 PM, andrija djurovic
5 matches
Mail list logo