由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - a R loop question
相关主题
Urgent R Question请教一个关于R的问题
这个R程序能帮改进一下吗?突然对直线拟合的R不明白起来了
请教R Code, 多谢!两个有关于R的小问题?
求问一个R apply 函数的问题请教一个概率题的思路
请教大神们关于bootstrapR question
R 扫描matrixR 画图问题求教
R:matrix【R求问】关于vector赋值
Help with R【R】保留matrix中某些值
相关话题的讨论汇总
话题: kf话题: rowptr话题: nowpos话题: dim话题: matrix
进入Statistics版参与讨论
1 (共1页)
g**r
发帖数: 425
1
My data looks like this:
0 0 0 1 0
0 1 0 0 0
0 0 1 0 0
...
Say each columns stands for a time, so I have 5 time points. I want to
select all the combination that the '1's happen on or after the current
matrix. So for 1st row, possible value would be 1 at time 4 or 5; for
second row, would be 2,3,4,5 and third row, 3, 4, 5.
Or, sth like the following code:
kf=c(0,1,0,0,0,1,1,0,0,0,0,0)
kf=matrix(kf,3)
for(i in 3:4)
for (j in 1:4)
for (k in 3:4){
kf[1
D******n
发帖数: 2836
2
This is definitely worth some baozis...
kf=c(0,1,0,0,0,1,1,0,0,0,0,0)
kf=matrix(kf,3)
nrow <- nrow(kf);
ncol <- ncol(kf);
oripos <- which(t(kf)==1,arr.ind=T)[,1];
nowpos <- oripos;
rowptr <- 1;
cat(nowpos,'\n');
while (rowptr<=nrow)
{
if ( nowpos[rowptr] { nowpos[rowptr] = nowpos[rowptr]+1;
rowptr = 1;
cat(nowpos,'\n');
}
else {
nowpos[rowptr] = oripos[rowptr];
rowptr = rowptr + 1;
}
}

【在 g**r 的大作中提到】
: My data looks like this:
: 0 0 0 1 0
: 0 1 0 0 0
: 0 0 1 0 0
: ...
: Say each columns stands for a time, so I have 5 time points. I want to
: select all the combination that the '1's happen on or after the current
: matrix. So for 1st row, possible value would be 1 at time 4 or 5; for
: second row, would be 2,3,4,5 and third row, 3, 4, 5.
: Or, sth like the following code:

q**j
发帖数: 10612
3
alternatively:
> y = t(apply(kf,1,cumsum))
> y
[,1] [,2] [,3] [,4]
[1,] 0 0 1 1
[2,] 1 1 1 1
[3,] 0 1 1 1
> x = rep(1:dim(kf)[2],dim(kf)[1])
> x
[1] 1 2 3 4 1 2 3 4 1 2 3 4
> x = matrix(x,byrow=TRUE,dim(kf)[1])
> x
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 1 2 3 4
[3,] 1 2 3 4
> x*y
[,1] [,2] [,3] [,4]
[1,] 0 0 3 4
[2,] 1 2 3 4
[3,] 0 2 3 4

【在 D******n 的大作中提到】
: This is definitely worth some baozis...
: kf=c(0,1,0,0,0,1,1,0,0,0,0,0)
: kf=matrix(kf,3)
: nrow <- nrow(kf);
: ncol <- ncol(kf);
: oripos <- which(t(kf)==1,arr.ind=T)[,1];
: nowpos <- oripos;
: rowptr <- 1;
: cat(nowpos,'\n');
: while (rowptr<=nrow)

g**r
发帖数: 425
4
Thanks, this works! Check your bank:)

【在 D******n 的大作中提到】
: This is definitely worth some baozis...
: kf=c(0,1,0,0,0,1,1,0,0,0,0,0)
: kf=matrix(kf,3)
: nrow <- nrow(kf);
: ncol <- ncol(kf);
: oripos <- which(t(kf)==1,arr.ind=T)[,1];
: nowpos <- oripos;
: rowptr <- 1;
: cat(nowpos,'\n');
: while (rowptr<=nrow)

g**r
发帖数: 425
5
Almost.
After adding k.list=list(kf)
for(i in 1:nrow(kf))k.list[[i]]=subset(k[i,],k[i,]!=0)
expand.grid(k.list)
Get the same results.
Thanks!

【在 q**j 的大作中提到】
: alternatively:
: > y = t(apply(kf,1,cumsum))
: > y
: [,1] [,2] [,3] [,4]
: [1,] 0 0 1 1
: [2,] 1 1 1 1
: [3,] 0 1 1 1
: > x = rep(1:dim(kf)[2],dim(kf)[1])
: > x
: [1] 1 2 3 4 1 2 3 4 1 2 3 4

1 (共1页)
进入Statistics版参与讨论
相关主题
【R】保留matrix中某些值请教大神们关于bootstrap
SAS code question, special two do loopR 扫描matrix
how to get a length of a vector in SAS/IMLR:matrix
请问下SAS执行中有什么单步执行之类的命令吗? 为什么我的proc iml里设置的参数t无论怎么改,结果都不变呢?Help with R
Urgent R Question请教一个关于R的问题
这个R程序能帮改进一下吗?突然对直线拟合的R不明白起来了
请教R Code, 多谢!两个有关于R的小问题?
求问一个R apply 函数的问题请教一个概率题的思路
相关话题的讨论汇总
话题: kf话题: rowptr话题: nowpos话题: dim话题: matrix