subset function

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

subset function

AppleJax
This post has NOT been accepted by the mailing list yet.
I have 6 datasets that have approximately 30 columns (TIMESTAMP, Point_1, Point_2, ..., Point_n), and each of these point variables contain thousands of temperature observations. Its current form is useless to me, I need to take a subset of each column for temperatures greater to or equal to 40 C while preserving the shape of the data curve.  So far, I have only been able to do this manually by extracting each column separately (see below). Is there a function that I could write that would systematically go through each column, selecting and saving only those observations that are >=40 with their time stamp? The current way that I'm going is quite tedious and I couldn't find any posts that covered this .

T1=read.table("C:/Users/......../10A_analysis_L1.txt",header=TRUE)
# select rows >= 40
L1T1=subset(T1,select=c("TIMESTAMP","Point_1"))
attach(L1T1)
rL1T1=L1T1[Point_1>=40,]

Any help is greatly appreciated. Thank you.

Reply | Threaded
Open this post in threaded view
|

Re: subset function

Rui Barradas
This post has NOT been accepted by the mailing list yet.
Hello,

Here are two solutions.

The first creates a logical matrix 'inx1' that you can use with 'subset'
The second creates a list of indices that you can use to directly access the data frame rows you want.

set.seed(1234)
tst <- Sys.time()+1:10
p1  <- runif(10, 30, 50)
p2  <- sample(35:45, 10, T)

dfr <- data.frame(TS=tst, p1=p1, p2=p2)
dfr

inx1 <- apply(dfr[-1], 2, function(x) x >= 40)
inx2 <- apply(dfr[-1], 2, function(x) which(x >= 40))

for(i in 1:ncol(inx1))
        print(subset(dfr, subset=inx1[, i], select=c(1, i+1)))

for(i in 1:length(inx2))
        print(dfr[inx2[[i]], c(1,i+1)])

I hope this helps

Rui Barradas
Reply | Threaded
Open this post in threaded view
|

Re: subset function

AppleJax
This post has NOT been accepted by the mailing list yet.
Thank you very much! That worked like a dream.
Paul

----- Original Message -----
From: "Rui Barradas [via R]" <[hidden email]>
To: "AppleJax" <[hidden email]>
Sent: Wednesday, January 4, 2012 4:44:52 PM
Subject: Re: subset function

Hello,

Here are two solutions.

The first creates a logical matrix 'inx1' that you can use with 'subset'
The second creates a list of indices that you can use to directly access the data frame rows you want.

set.seed(1234)
tst <- Sys.time()+1:10
p1 <- runif(10, 30, 50)
p2 <- sample(35:45, 10, T)

dfr <- data.frame(TS=tst, p1=p1, p2=p2)
dfr

inx1 <- apply(dfr[-1], 2, function(x) x >= 40)
inx2 <- apply(dfr[-1], 2, function(x) which(x >= 40))

for(i in 1:ncol(inx1))
print(subset(dfr, subset=inx1[, i], select=c(1, i+1)))

for(i in 1:length(inx2))
print(dfr[inx2[[i]], c(1,i+1)])

I hope this helps

Rui Barradas





If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/subset-function-tp4263093p4263238.html 
To unsubscribe from subset function, click here .
NAML