Re: [R] delete selecting rows and columns

2007-03-06 Thread Kaskelma, Heikki
A one-dimensional delete might be faster:

n - 4000
m - 0.01
matr - matrix(1:(n^2), n, n)
excl_r - sample(n, m*n)
incl_r - setdiff(1:n, excl_r)
excl_c - sample(n, m*n)
incl_c - setdiff(1:n, excl_c)
system.time(matr[-excl_r, -excl_c])
system.time(
{ m4 - matr[outer(incl_r, incl_c, function(i, j) i + n*(j - 1)) ]
  dim(m4) - c(length(incl_r), length(incl_c))
}  )


Heikki Kaskelma


jastar wrote:
 
 Hi,
 I'm working with a big square matrix (15k x 15k) and I have 
 some trouble.
 I want to delete selecting rows and columns.
 I'm using something like this:
 
 sel_r=c(15,34,384,985,4302,6213)
 sel_c=c(3,151,324,3384,7985,14302)
 matrix=matrix[-sel_r,-sel_c]
 
 but it works very slow.
 Does anybody know how to make it in faster way?

__
R-help@stat.math.ethz.ch 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] Conditional Sum

2007-02-06 Thread Kaskelma, Heikki
Yes, consider:

df=data.frame(Clinic=rep(c(A, B, C), 2),
  Rep=c(1, 1, 1, 2, 2, 2),
  colM1=c(1, 0, 0, 1, -1, 1),
  ColM2=c(0, -1, -1, 1, 0, 0),
  ColM3=c(0, 0, 1, -1, 0, 1),
  ColM40=c(1, 0, -1, 0, 1, -1)
 )
Clinic=1; Rep=2
ff=function(x, v) sum(x == v)
Dataframe1=aggregate(df[, -c(Clinic, Rep)], df[Clinic], ff, 1)
names(Dataframe1)[1]=Clinic
Dataframe2=aggregate(df[, -c(Clinic, Rep)], df[Clinic], ff, -1)
names(Dataframe2)[1]=Clinic
Dataframe1
Dataframe2


Heikki Kaskelma

-- 
Bert Jacobs kirjoitti jokin aikaa sitten:
 I would like to make a conditional sum for certain columns in 
 a dataframe.
 
 This is how my dataframe looks like:
 
 Clinic Rep ColM1 ColM2 ColM3 ... ColM40
 A  1 10  01
 B  1 0-1 00
 C  1 0-1 1-1 
 A  2 11  -1   0
 B  2 -1   0  01 
 C  2 10  1   -1
 
 I would like to have two new dataframes so that
 Dataframe1: with the count of all 1
 
 Clinic  ColM1  ColM2 ColM3 .. ColM40
 A2  10 1
 B0  00 1
 C1  02 0
 
 Dataframe2: with the count of all -1
 
 Clinic  ColM1  ColM2 ColM3 .. ColM40
 A0  01 0
 B1  10 0
 C0  10 2
 
 
 Is there an easy way to achieve this.
 Thx,
 Bert

__
R-help@stat.math.ethz.ch 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] Can this loop be delooped?

2007-02-02 Thread Kaskelma, Heikki
Consider

na=43; nb=5; x=1:na
ns=rep(na %/% nb, nb) + (1:nb = na %% nb)
split(x, rep(1:nb, ns))


Heikki Kaskelma

On Fri, 2 Feb 2007, jim holtman [EMAIL PROTECTED] wrote:
This might do what you want:

 # test data
 x - 1:43
 nb - 5  # number of subsets
 # create vector of lengths of subsets
 ns - rep(length(x) %/% nb, nb)
 # see if we have to adjust counts of initial subsets
 if ((.offset - length(x) %% nb) != 0) ns[1:.offset] = ns[1:.offset] +
1
 # create the subsets
 split(x, rep(1:nb,ns))

__
R-help@stat.math.ethz.ch 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] How to optimize this loop ?

2007-01-23 Thread Kaskelma, Heikki
Given a series of observations, I want to know how many consecutive
past
observations are below the last one.

prune=function(m)
{ mr=rev(m)
  ms=cumsum(mr  mr[1])
  sum(seq_along(ms) - ms == 1) - 1
}

prune(c(3, 4, 10, 14, 8, 3, 4, 8, 9))  # 4
prune(c(3, 4, 10, 14, 8, 3, 4, 11, 9)) # 0


Heikki Kaskelma

__
R-help@stat.math.ethz.ch 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] indexing question

2006-11-14 Thread Kaskelma, Heikki
idx = c(31, 36, 41, 61, 66, 71, 91, 96, 101, 121, 126, 131)
temp = 1:150
res = temp[idx]
dim(res) = c(3, length(res) %/% 3)
res = t(rbind(res, res[2, ] - res[1, ], res[3, ] - res[2, ]))
res


Heikki Kaskelma

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Leeds, Mark (IED)
Sent: Monday, November 13, 2006 10:06 PM
To: r-help@stat.math.ethz.ch
Subject: [R] indexing question

I have the following set of indices, call it idx, that correspond to the
indices of a vector say temp.

 [1]   31   36   41   61   66   71   91   96  101  121  126  131  151
156  161  181  186  191  211  216  221  241  246  251  271  276  281
301  306  311  331  336  341  361  366
 [36]  371  391  396  401  421  426  431  451  456  461  481  486  491
511  516  521  541  546  551  571  576  581  601  606  611  631  636
641  661  666  671  691  696  701  721
 [71]  726  731  751  756  761  781  786  791  811  816  821  841  846
851  871  876  881  901  906  911  931  936  941  961  966  971  991
996 1001 1021 1026 1031 1051 1056 1061
[106] 1081 1086 1091  1116 1121 1141 1146 1151 1171 1176 1181 1201
1206 1211 1231 1236 1241 1261 1266 1271 1291 1296 1301 1321 1326 1331
1351 1356 1361 1381 1386 1391 1411 1416
[141] 1421


I want to calculate temp[36] - temp[31] and temp[41] - temp[36]

Similarly, temp[66] - temp[61] and temp[71] - temp[66]
.
.
.
.
Similarly temp[1416]-temp[1411]
  temp[1421] - temp[1416]


I'm doing this because the above subractions represent pairs of returns
and the correlations between them wil be calculated eventually.

In other words, eventually I will have

X_36_31  ( i.e : temp[36] - temp[31] )
X_66-61
X_96-91
.
.
.
.
.
.
.
X_1411-1416 

as one vector and

Y_41-36
Y_71-66
Y_101-96
.
.
.
.
.
Y_1416_1421

as another vector.

and will calculate the correlation between the two vectors in order to
get one number.


The point is I am  really only using the indices 31, 61, 91 etc as
anchor's so a regular diff(temp[idx]) won't work because it will diff
all 
the elements that are next to each other ? This is a weird problem. I'm
still thinking about it. I'm hoping to figure it out before someone
sends me something but I won't mind so much if I get an external
solution first. I have no pride.


This is not an offer (or solicitation of an offer) to
buy/se...{{dropped}}

__
R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.