Hi,
I've read in a csv file (test.csv) which gives me the following table:
Hin1 Hin2 Hin3 Hin4 Hin5 Hin6
HAI1 9534.83 4001.74 157.16 3736.93 484.60 59.25
HAI2 13272.48 1519.88 36.35 33.64 46.68 82.11
HAI3 12587.71 5686.94 656.62 572.29 351.60 136.91
HAI4 15240.81 10031.57 426.73 275.29 561.30 302.38
HAI5 15878.32 10517.14 18.93 22.00 16.91 21.17
I would like to find a way of finding the 1-pnorm of each value in the table
based on the mean and sd of the data only in the column in which the value lies.
I can do it using a for loop, but would like to know if it can be done using
e.g. apply or something similar, so that the whole table is printed out with the
1-pnorm values.
1-pnorm(test[,1],mean([,1]), sd([,1])) gives me the values for col1 only, but
that's as far as I've got.
tia
p.s. I know I'm asking a lot, but ideally, I'd like to print out the
table with those 1-pnorm values only if they are in the right hand tail (i.e.
>= mean) and if not nothing or NA be written.
---------------------------------
[[alternative HTML version deleted]]
> I've read in a csv file (test.csv) which gives me the following table: > > Hin1 Hin2 Hin3 Hin4 Hin5 Hin6 > HAI1 9534.83 4001.74 157.16 3736.93 484.60 59.25 > HAI2 13272.48 1519.88 36.35 33.64 46.68 82.11 > HAI3 12587.71 5686.94 656.62 572.29 351.60 136.91 > HAI4 15240.81 10031.57 426.73 275.29 561.30 302.38 > HAI5 15878.32 10517.14 18.93 22.00 16.91 21.17 > > I would like to find a way of finding the 1-pnorm of each value in > the table based on the mean and sd of the data only in the column in > which the value lies. I can do it using a for loop, but would like > to know if it can be done using e.g. apply or something similar, so > that the whole table is printed out with the 1-pnorm values.Try: nrows <- 5 mm <- matrix(rnorm(30),nrow=nrows) sd.by.col <- apply(mm,2,sd) mean.by.col <- apply(mm,2,mean) values <- 1-mapply(pnorm, q=as.vector(mm), mean=rep(mean.by.col, nrows)), sd=rep(sd.by.col, nrows))) values <- matrix(values, nrow=5)> p.s. I know I'm asking a lot, but ideally, I'd like to print out > the table with those 1-pnorm values only if they are in the right > hand tail (i.e. >= mean) and if not nothing or NA be written.values[values<.5] <- NA Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
This is a bit ugly but I think it works.
===============================================
myf <- function(x) 1-pnorm(x,mean(x), sd(x))
results <- apply(test, 2, myf)
mymeans <- apply(test, 2, mean); mymeans
for (i in 1:length(test)){
test[,i][test[,1]>=mymeans[i]] <- NA
}
results[is.na(test)] <- NA
===================================================--- Jabez Wilson <jabezwuk
at yahoo.co.uk> wrote:
> Hi,
>
> I've read in a csv file (test.csv) which gives me
> the following table:
>
> Hin1 Hin2 Hin3 Hin4 Hin5
> Hin6
> HAI1 9534.83 4001.74 157.16 3736.93 484.60 59.25
> HAI2 13272.48 1519.88 36.35 33.64 46.68 82.11
> HAI3 12587.71 5686.94 656.62 572.29 351.60 136.91
> HAI4 15240.81 10031.57 426.73 275.29 561.30 302.38
> HAI5 15878.32 10517.14 18.93 22.00 16.91 21.17
>
> I would like to find a way of finding the 1-pnorm
> of each value in the table based on the mean and sd
> of the data only in the column in which the value
> lies. I can do it using a for loop, but would like
> to know if it can be done using e.g. apply or
> something similar, so that the whole table is
> printed out with the 1-pnorm values.
> 1-pnorm(test[,1],mean([,1]), sd([,1])) gives me
> the values for col1 only, but that's as far as I've
> got.
>
> tia
>
> p.s. I know I'm asking a lot, but ideally, I'd
> like to print out the table with those 1-pnorm
> values only if they are in the right hand tail (i.e.
> >= mean) and if not nothing or NA be written.
>
>
>
> ---------------------------------
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
>
<Richard.Cotton <at> hsl.gov.uk> writes:
[snip]
> Try:
> nrows <- 5
> mm <- matrix(rnorm(30),nrow=nrows)
> sd.by.col <- apply(mm,2,sd)
> mean.by.col <- apply(mm,2,mean)
> values <- 1-mapply(pnorm, q=as.vector(mm), mean=rep(mean.by.col,
> nrows)), sd=rep(sd.by.col, nrows))) values <- matrix(values, nrow=5)
>
> > p.s. I know I'm asking a lot, but ideally, I'd like to print
out
> > the table with those 1-pnorm values only if they are in the right
> > hand tail (i.e. >= mean) and if not nothing or NA be written.
>
> values[values<.5] <- NA
>
I'm not sure, but I think that
nrows <- 5
mm <- matrix(rnorm(30),nrow=nrows)
pnorm(scale(mm),lower.tail=FALSE)
values[values<.5] <- NA
will do the same thing.
lower.tail=FALSE is a little more accurate than 1-pnorm(...)
cheers
Ben Bolker
Brilliant! I am in awe....
Thanks for the other contributions
Jab
______________________________________________
R-help@r-project.org 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.
---------------------------------
[[alternative HTML version deleted]]