Dear S+ users
My original problem was:
I have a data frame in which the first column indicates the day number, the second column observation number and the third results of observations. It may look like:
Dn On R1
1 1 3
1 2 4
1 3 5
2 4 6
2 5 7
2 6 8
To find minimum value of R1 for each day is easy:
min.valR1<-Tapply(R1,Dn,min)
but how can I get not only the vector of minimum values, but also a vector of observation numbers for which the minimum occurred I am using S+ 4.5 for Windows.
I would like to thank James Holtman, Sundar Dorai-Raj, Patrick Burns Mark
Leeds, Joseph Verducci, Spancer Graves, and Jingshan Zhangfor their prompt
reply.
The codes of James and Sundar worked at once and gave me exactly what I wanted.
James wrote:
A 'trick' that I always use is to work with the 'index' of the data so that
I can create a function that will return any combination of the other data
items in a data frame or any other structure:
x.1
Dn On R1
1 1 1 3
2 1 2 4
3 1 3 5
4 2 4 6
5 2 5 7
6 2 6 8
x.2 <- sapply(split(1:nrow(x.1), x.1$Dn), function(x){ # 'split' the
indices
+ c(Dn=x.1$Dn[x[1]], R1=min(x.1$R1[x])) # construct the data you
want to return
+ })
t(x.2) # transpose to get in the correct order
Dn R1
1 1 3
2 2 6
Sundar proposed:
dat <- data.frame(Dn = c(1, 1, 1, 2, 2, 2),R1 = c(1, 0, 3, 5, 6, 4))
by(dat$R1, dat$Dn, function(x)
{ m <- min(x)
c(m, which(x == m))
})
Thank you once again and best regards
Janusz
|