[R] help with data layout

Stephen Tucker brown_emu at yahoo.com
Thu Jul 17 18:29:15 CEST 2008


Hi, hope this will help:

txt <- "Col1,    Col2   
A,        3
,         2
,        3
B,        4
,         5
,         4
C,        1
,         4
,         3"

## read data
dat <- read.csv(textConnection(txt),na.string="")
## fill in empty cells with correct category
dat$Col1[] <-
  Reduce(function(x,y) c(x,ifelse(is.na(y),tail(x,1),y)),dat$Col1)
## calculate mean and standard deviation
mat <- t(sapply(split(dat$Col2,f=dat$Col1),function(X)
              c(mean=mean(X),sd=sd(X))))
## look at results (stored in a matrix)
> print(mat)
      mean        sd
A 2.666667 0.5773503
B 4.333333 0.5773503
C 2.666667 1.5275252



----- Original Message ----
From: Iain Gallagher <iaingallagher at btopenworld.com>
To: r-help at stat.math.ethz.ch
Sent: Thursday, July 17, 2008 8:50:42 AM
Subject: [R] help with data layout

Hello list

I have been given some Excel sheets with data laid like this:

Col1    Col2    
A         3
           2
           3
B         4
           5
           4
C         1
           4
           3

I was hoping to import this into R as a csv and then get the mean and SD for each letter in column 1.

Could someone give me some guidance on best to approach this?

Thanks

Iain

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



More information about the R-help mailing list