[R] How to fill out some columns?

2008-04-11 Thread Jorge Velez
Dear R users,

I'm working with 2 data sets which look like (for example) dx and dy in the
next code:

# Seed
set.seed(4)

# First data frame
dx=matrix(rnorm(6*5),ncol=6)
colnames(dx)=LETTERS[1:6]

# Second data frame
dy=matrix(rnorm(3*5),ncol=3)
colnames(dy)=c('A','C','E')


As you will notice, some columns in both data sets have the same names. At
the end, what I need is something like:


> res
  A B  C D  E F
[1,]  0.1534642 0 -0.4045198 0  1.3437086 0
[2,]  1.0519326 0 -0.2274054 0  0.1815354 0
[3,] -0.7542112 0  0.9340962 0  1.2925123 0
[4,] -1.4821891 0 -0.4658959 0 -1.6880486 0
[5,]  0.8611319 0 -0.6375435 0 -0.8209936 0


where  columns "A", "C" and "E" are the dy columns and columns "B" and "D"
are zeros because, when we compare dx and dy, they are not present. Any
suggestion would be greatly appreciated.

Thanks in advance,


Jorge

[[alternative HTML version deleted]]

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


Re: [R] Problem with NA data when computing standard error

2008-04-08 Thread Jorge Velez
Hi there,

Perhaps

se<-function(x)sqrt(var(x,na.rm=T)/sum(!is.na(x)))
object1<-as.data.frame.table(tapply(Data[Year=="1999"],list(Group[Year=="1999"],Season[Year=="1999"]),mean))
object2<-as.data.frame.table(tapply(Data[Year=="1999"],list(Group[Year=="1999"],Season[Year=="1999"]),se))


Hope this helps,

Jorge


On Tue, Apr 8, 2008 at 1:44 PM, LeCzar <[EMAIL PROTECTED]> wrote:

>
> Hey,
>
> I want to compute means and standard errors as two tables like this:
>
>  se<-function(x)sqrt(var(x)/length(x))
>
>
> object1<-as.data.frame.table(tapply(Data[Year=="1999"],na.rm=T,list(Group[Year=="1999"],Season[Year=="1999"]),mean))
>
>
> object2<-as.data.frame.table(tapply(Data[Year=="1999"],na.rm=T,list(Group[Year=="1999"],Season[Year=="1999"]),se))
>
> Part of the data is labeled as NA, therfore the na.rm=T. I succeed in
> creating the table for the means, but not for the standard errors. The
> message is "Error in FUN(X[[1L]], ...) : unused argument(s) (na.rm =
> TRUE)".
> Does anyone have an idea how to get the standard error computed?
>
> Thx!
>
> --
> View this message in context:
> http://www.nabble.com/Problem-with-NA-data-when-computing-standard-error-tp16569057p16569057.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> 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]]

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


Re: [R] how to check if a variable is preferentially present in a sample

2008-04-08 Thread Jorge Velez
Hi Tania,

I think it could be. I tried a solution based on your data set using a
chi-squared approach. Here is what I got:

# 
# Data set
set.seed(123)
d <- data.frame(cbind(val=rnorm(1:10)^2,
group=sample(LETTERS[1:5],100,repl=TRUE)))
d[,"val"]<-as.numeric(as.character(d$val))

# Ranking "d" in decreasing order based on "val" and counting the number of
observation in each group
TABLE=table(d[order(val,decreasing=TRUE),][1:10,"group"])
TABLE

A B C D E
3 2 3 1 1

# Chi-squared
cht=chisq.test(TABLE)
cht

Chi-squared test for given probabilities

data:  TABLE
X-squared = 2, df = 4, p-value = 0.7358

cht$p.value
[1] 0.7357589


Hope this helps,


Jorge


On Tue, Apr 8, 2008 at 11:24 AM, Tania Oh <[EMAIL PROTECTED]> wrote:

> Dear All,
>
> I do apologise if this question is out of place for this list but I've
> tried searching mailing lists and read "Introductory Statistics with
> R" by Peter Dalgaard, but couldn't find any hints on solving my
> question below:
>
> I have a data frame (d) of values which I will rank in decreasing
> order of "val". Each value belongs to a group, either 'A', 'B', 'C',
> 'D', or 'E'.  I then take the first 10 entries in data frame 'd'  and
> count the number of occurrences for each of the groups.  I want to
> test if certain groups occur more frequently than by chance in my
> first 10 entries. Would a chi-square test or a hypergeometric test be
> more suitable? If neither, what would be an alternative solution in
> R?  Below is my data:
>
>
> ## data
> L5 <- LETTERS[1:5]
> d <- data.frame(cbind(val= rnorm(1:10)^2, group=sample(L5,100,
> repl=TRUE)))
>
> str(d)
> ##'data.frame': 100 obs. of  2 variables:
> ##$ val  : Factor w/ 10 levels "0.000169268449333046",..: 10 3 5 6 1 2
> 7 8 4 9 ...
> ##$ group: Factor w/ 5 levels "A","B","C","D",..: 4 4 4 5 3 1 5 2 1
> 2 ...
>
>
> Many thanks in advance and apologies again,
> tania
>
> D. phil student
> Department of Physiology, Anatomy and Genetics
> University of Oxford
>
> __
> 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]]

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


Re: [R] test for the variance of a normal population

2008-04-07 Thread Jorge Velez
Dear Alexandra,

Perhaps you can use a different approach but I think it works:


# Chi-suared CI
var.ci=function(x,alpha=0.05){
n=length(x)-sum(is.na(x))
s2=var(x,na.rm=T)
li=s2*(n-1)/qchisq(1-alpha/2,n-1)
ls=s2*(n-1)/qchisq(alpha/2,n-1)
c(li,ls)
}

# Example
set.seed(123)
y=rnorm(1000,25,2)
var.ci(y) # 3.610426 4.302965


# Bootstrap CI
var.bs=function(x,FUN=var,alpha=0.05,NSIM=1000){
temp=matrix(rep(x,NSIM),ncol=NSIM)
temp2=apply(temp,2,sample,replace=TRUE)
vars=apply(temp2,2,var)
quantile(vars,probs=c(alpha/2,1-alpha/2))
}


var.bs(y)  # 3.618220 4.274773


I hope this helps,

-- 
JIV


On Mon, Apr 7, 2008 at 10:13 AM, Alexandra Ramos <[EMAIL PROTECTED]> wrote:

> Hi,
>
>
>
> Does anyone know an instruction to perform a test (or to construct a
> confidence interval) for the variance of a normal population (distribution).
>
> The test statistic is  (n-1)*S´^2/sigma^2  and has chi square (n-1)
> distribution.
>
>
>
> Thank you,
>
> Alexandra
>
>
>
>
>
>
>[[alternative HTML version deleted]]
>
>
> __
> 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]]

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


Re: [R] how to save a chart as ps in R?

2008-03-11 Thread Jorge Velez
Hi Samuel,

Sure. Ckeck  ?savePlot or ?postscript. I prefer the first one.

HTH


JIV


On Wed, Mar 12, 2008 at 12:04 AM, Samuel <[EMAIL PROTECTED]> wrote:

> Hi guys,
>
> I remember that we had some ways to save a chart as ps file, which can be
> used by latex directly. I can't find it now. Can you guys give me some
> hints?
>
> Thanks very much.
>
> --
> Samuel Wu
> http://webclipping.com.cn
>
>[[alternative HTML version deleted]]
>
> __
> 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]]

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


[R] Replacing text

2008-03-11 Thread Jorge Velez
Hi Luciana,

Try this:

yourData[,1] <- sapply(yourData[,1], function(x){
 x=as.character(x)
x[which(x=="Toyota2")]<-"Scion"
x
}
)

# Example
set.seed(123)
x=c("Jeep","Nissan", "Toyota1", "Toyota2")
y=rnorm(4)
DATA=data.frame(x,y)
DATA
 x   y
1Jeep -0.56047565
2  Nissan -0.23017749
3 Toyota1  1.55870831
4 Toyota2  0.07050839


DATA[,1] <- sapply(DATA[,1], function(x){
 x=as.character(x)
x[which(x=="Toyota2")]<-"Scion"
x
}
)

DATA
x   y
1Jeep -0.56047565
2  Nissan -0.23017749
3 Toyota1  1.55870831
4   Scion  0.07050839


HTH,

Jorge



On Tue, Mar 11, 2008 at 2:01 PM, Suran, Luciana @ Torto Wheaton Research <
[EMAIL PROTECTED]> wrote:

> Sorry, another newbie question :-(
>
>
>
>
>
> I loaded a data set with 10 rows and 30 columns. The first column is
> characters for names of car manufacturers:
>
>
>
> Jeep
>
> Nissan
>
> Toyota1
>
> Toyota2
>
> Etc.
>
>
>
> How can I replace "Toyota2" with "Scion"?
>
>
>
> Thanks again
>
>
>
>
>
>
>
>
>
>
>[[alternative HTML version deleted]]
>
> __
> 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]]

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