[R] finding the minimum positive value of some data

2007-09-10 Thread dxc13

useRs,

I am looking to find the minimum positive value of some data I have. 
Currently, I am able to find the minimum of data after I apply some other
functions to it:

 x
 [1]  1  0  1  2  3  3  4  5  5  5  6  7  8  8  9  9 10 10

 sort(x)
 [1]  0  1  1  2  3  3  4  5  5  5  6  7  8  8  9  9 10 10

 diff(sort(x))
 [1] 1 0 1 1 0 1 1 0 0 1 1 1 0 1 0 1 0

 min(diff(sort(x)))
[1] 0

The minimum is given as zero, which is clearly true, but I am interested in
only the positive minimum, which is 1.  Can I find this by using only 1 line
of code, like I have above? Thanks!

dxc13
-- 
View this message in context: 
http://www.nabble.com/finding-the-minimum-positive-value-of-some-data-tf4417250.html#a12599319
Sent from the R help mailing list archive at Nabble.com.

__
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] using temporary arrays in R

2007-09-03 Thread dxc13

useR's,

Is there a way to create a temporary array (or matrix) in R to hold values,
then drop or delete that temporary array from memory once I do not need it
anymore?

I am working with multidimensional arrays/matrices and I frequently perform
multiple operations on the same matrix and rename it to be another object. 
I want to be able to delete the older versions of the array/matrix to free
up space. 

Thank you. 
-- 
View this message in context: 
http://www.nabble.com/using-temporary-arrays-in-R-tf4372367.html#a12462219
Sent from the R help mailing list archive at Nabble.com.

__
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] memory.size help

2007-08-31 Thread dxc13

I keep getting the 'memory.size' error message when I run a program I have
been writing.  It always it cannot allocate a vector of a certain size.  I
believe the error comes in the code fragement below where I have multiple
arrays that could be taking up space.  Does anyone know a good way around
this?

w1 - outer(xk$xk1, data[,x1], function(y,z) abs(z-y))
w2 - outer(xk$xk2, data[,x2], function(y,z) abs(z-y))
w1[w1  d1] - NA
w2[w2  d2] - NA
i1 - ifelse(!is.na(w1),yvals[col(w1)],NA)
i2 - ifelse(!is.na(w2),yvals[col(w2)],NA)
zk - numeric(nrow(xk))  #DEFININING AN EMPTY VECTOR TO HOLD ZK VALUES
for(x in 1:nrow(xk)) {
k - intersect(i1[x,], i2[x,])
zk[x] - mean(unlist(k), na.rm = TRUE)
}
xk$zk - zk
data - na.omit(xk)
-- 
View this message in context: 
http://www.nabble.com/memory.size-help-tf4359846.html#a12425401
Sent from the R help mailing list archive at Nabble.com.

__
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] memory.size help

2007-08-31 Thread dxc13

I have 512 MB RAM with a 1.5 GHz processor.  The dataset I am working with
increases with size with every iteration of the function that I am writing. 
R can handle the input data which is about 10,000 records, and a single
iteration with 19,500 observations.  After this, I get the memory.size()
error message.
I have only written 1 other R function so I am not too familiar with
optimizing code.  Do you have any suggestions?


Andris Jankevics wrote:
 
 You havn't said anithing about your OS and version. But basicly I means,
 that 
 you don't have enoigh RAM or swap memory aviable on your system. How large 
 dataset you have?
 
 1. You can optimise your code. :) 
 
 or
 
 2. You can just add more RAM to your system, or you can add more swap
 space on 
 your HDD. Work with data in swap will be really slow.
 
 In linux OS you can add additional swap space, by these commmands:
 
 2 GB file:
 
 dd if=/dev/zero of=/swapfile1 bs=1024 count=2097152
 mkswap /swapfile1
 swapon /swapfile1
 
 
 
 On Friday 31 August 2007 15:27:58 dxc13 wrote:
 w1 - outer(xk$xk1, data[,x1], function(y,z) abs(z-y))
 w2 - outer(xk$xk2, data[,x2], function(y,z) abs(z-y))
 w1[w1  d1] - NA
 w2[w2  d2] - NA
 i1 - ifelse(!is.na(w1),yvals[col(w1)],NA)
 i2 - ifelse(!is.na(w2),yvals[col(w2)],NA)
 zk - numeric(nrow(xk))  #DEFININING AN EMPTY VECTOR TO HOLD ZK
 VALUES
 for(x in 1:nrow(xk)) {
 k - intersect(i1[x,], i2[x,])
 zk[x] - mean(unlist(k), na.rm = TRUE)
 }
 xk$zk - zk
 data - na.omit(xk)
 
 
 
 -- 
 Andris Jankevics
 Assistant
 Department of Medicinal Chemistry
 Latvian Institute of Organic Synthesis
 Aizkraukles 21, LV-1006, Riga, Latvia
 
 __
 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.
 
 

-- 
View this message in context: 
http://www.nabble.com/memory.size-help-tf4359846.html#a12430698
Sent from the R help mailing list archive at Nabble.com.

__
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] a faster and shorter way to perform calculations?

2007-08-29 Thread dxc13

This is a continuation from a previous posting of mine:

The following algorithm below is what I want to accomplish:
Z(xk) = Average(Yi, i belongs to Ik), where Ik contains all i such that for
each j, |Xi,j - xkj│≤ 2.  Here, j = 1, 2 and i corresponds to the elements
in each X and/or xk

data
x1   x2   y
11 2
22 6
33 12

Now, consider a second data frame or matrix (xk):
xk1   xk2
0.50.5
1.00.5
1.50.5
2.00.5
0.51
1.01
1.51
2.01
0.51.5
1.01.5
1.51.5
2.01.5
0.52
1.02
1.52
2.02 

In the end, there should be matrix or data frame with 3 columns, xk1, xk2,
and z (the result of the algorithm).  Having any NA's as an answer is just
fine.  Here is my code to get this to get this to work, it is lengthy and I
am looking for a way to cut it down and get the same results.

w1 - outer(xk$xk1, data[,x1], function(y,z) abs(z-y))
w2 - outer(xk$xk2, data[,x2], function(y,z) abs(z-y))
w1[w1  d1] - NA 
w2[w2  d2] - NA
i1 - ifelse(!is.na(w1),col(w1),NA)
i2 - ifelse(!is.na(w2),col(w2),NA)

#NOW COMPARE i1 AND i2 BY ROWS...IF THEY HAVE A VALID NUMBER IN 
#THE SAME POSITION (RxC) THEN EXTRACT THE Z-VALUE CORRESPONDING
#TO THE COLUMN NUMBER 
all - cbind(i1,i2)
Ik - lapply(as.data.frame(t(all)), function(x)
data$y[unique(x[duplicated(x)])])
zk - unlist(lapply(Ik, mean, na.rm = TRUE))
z - cbind(zk)
#FINAL DATA FRAME WILL CONSIST OF xk (XK1 and XK2), ZK
xk$z - z   
df - as.data.frame(sapply(xk,rbind))

I hope my question makes sense and someone can help find a way to reduce the
amount of code here.

I have attached a text file with better numbers to try this with.

Thank you,
dxc13 http://www.nabble.com/file/p12387673/test.txt test.txt 
-- 
View this message in context: 
http://www.nabble.com/a-faster-and-shorter-way-to-perform-calculations--tf4347973.html#a12387673
Sent from the R help mailing list archive at Nabble.com.

__
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] alternate methods to perform a calculation

2007-08-28 Thread dxc13

Consider a data frame (x) with 2 variables, x1 and x2, having equal values. 
It looks like:

x1   x2
11
22
33

Now, consider a second data frame (xk):
xk1   xk2
0.50.5
1.00.5
1.50.5
2.00.5
0.51
1.01
1.51
2.01
0.51.5
1.01.5
1.51.5
2.01.5
0.52
1.02
1.52
2.02

I have written code to calculate some differences between these two data
sets; the main idea is to subtract off each element of xk1 from each value
of x1, and similarly for xk2 and x2.  This is what I have:

w1 - array(NA,dim=c(nrow(xk),length(x$x1)))
w2 - array(NA,dim=c(nrow(xk),length(x$x2))) 
for (j in 1:nrow(xk)) {
w1[j,] - abs(x$x1-xk$xk1[j])
w2[j,] - abs(x$x2-xk$xk2[j])
}

Is there  a way to do the above calculation without use of a FOR loop? 
Thank you

Derek


-- 
View this message in context: 
http://www.nabble.com/alternate-methods-to-perform-a-calculation-tf4344469.html#a12376906
Sent from the R help mailing list archive at Nabble.com.

__
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] extracting duplicated elements

2007-08-23 Thread dxc13

Can anyone help me solve this problem...thanks!

Consider a data frame, namely v, as such:
 v
   X1 X2 X3 X4 X5 X1 X2 X3 X4 X5
x1  1  2 -1 -1 -1  1  2 -1 -1 -1
y1  1  2 -1 -1 -1  1  2  3 -1 -1

What I would like to do is to create an array or data frame with only the
elements that appear in the data frame more than once and are = 0.

I try this...
 v[v=0]
[1] 1 1 2 2 1 1 2 2 3

which returns all = 0 elements, but they are not in their respective rows
from the original data frame.  I have tried using the duplicated()
function and can't seem to get it to work correctly.

Essentially, the outcome I am trying to get is a df or array looking like:

step 1...achieve this out of original df
[1] 1 2 1 2
[2] 1 2 1 2 3

(the blank element in row 1, position 5 can be just be NA)

step 2...take the above and get this...only the duplicated elements
[1] 1 2
[2] 1 2

-- 
View this message in context: 
http://www.nabble.com/extracting-duplicated-elements-tf4318034.html#a12295213
Sent from the R help mailing list archive at Nabble.com.

__
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.