[R] help with data layout

jim holtman jholtman at gmail.com
Thu Jul 17 18:33:43 CEST 2008


Does this do at least the means for you:

> x <- read.csv(textConnection("Col1 ,   Col2
+ A    ,     3
+      ,     2
+      ,     3
+ B    ,     4
+      ,     5
+     ,      4
+ C   ,      1
+     ,      4
+     ,      3"), strip.white=TRUE)
> x
  Col1 Col2
1    A    3
2         2
3         3
4    B    4
5         5
6         4
7    C    1
8         4
9         3
> # replace blanks with NAs in first column
> is.na(x$Col1) <- x$Col1 == ''
> require(zoo)
> x$Col1 <- na.locf(x$Col1)
> x
  Col1 Col2
1    A    3
2    A    2
3    A    3
4    B    4
5    B    5
6    B    4
7    C    1
8    C    4
9    C    3
> aggregate(x$Col2, list(x$Col1), FUN=mean)
  Group.1        x
1       A 2.666667
2       B 4.333333
3       C 2.666667
>


On Thu, Jul 17, 2008 at 11:50 AM, Iain Gallagher
<iaingallagher at btopenworld.com> wrote:
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list