[R] Dataframe manipulation
arun
smartpink111 at yahoo.com
Fri Mar 29 22:14:53 CET 2013
Hi Adam,
I hope this is what you wanted:
dat1<- read.csv("example.csv",sep="\t",stringsAsFactors=FALSE)
str(dat1)
#'data.frame': 102 obs. of 5 variables:
# $ species : chr "B. barbastrellus" "E. nilssonii" "H. savii" "M. alcathoe" ...
# $ period : chr "dusk" "dusk" "dusk" "dusk" ...
# $ treatment: chr "control" "control" "control" "control" ...
# $ no.files : int 16 1 9 13 1 49 6 3 4 0 ...
# $ expected : logi NA NA NA NA NA NA ...
dat2<-within(dat1,{expected<-ave(no.files,species,treatment,FUN=mean)})
head(dat2)
# species period treatment no.files expected
#1 B. barbastrellus dusk control 16 14.333333
#2 E. nilssonii dusk control 1 1.000000
#3 H. savii dusk control 9 4.666667
#4 M. alcathoe dusk control 13 13.333333
#5 M. bechsteinii dusk control 1 5.666667
#6 M. brandtii dusk control 49 63.000000
A.K.
________________________________
From: adam bosworth <english.fellow at hotmail.com>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com>
Sent: Friday, March 29, 2013 2:29 PM
Subject: RE: Dataframe manipulation
Hey,
Thanks for the responce. I'm not sure if you messaged me on the forum or emailed me, but the only way I could get back to you was email, so hope that's alright.
I've attached part of the dataset into a csv file for you to look at.
in cell E2 I've given an example output I'd like in the dataframe, by summing values of '16', '25' and '2' from cells D2, D12 and D22 respectively and then dividing this by 3.
Thanks for the help, much appreshiated!
> Date: Fri, 29 Mar 2013 10:24:31 -0700
> From: smartpink111 at yahoo.com
> To: english.fellow at hotmail.com
> Subject: Dataframe manipulation
>
> HI,
> Is it possible to post a small example dataset and also the output dataset you wanted? In that way, it will be much more easier to understand what you meant.
> <quote author='englishfellow'>
> New to R and struggling with dataframe commands, any help will be much
> appreshiated.
>
> I have an existing dataframe call it 'df' with 4 colums and I have added a
> 5th column which I need to fill, the conditions of which are as follows:
>
> for each row in column 5, I need it to look at column 1 and find all data
> which are equal to that present in that row. I then need to look through
> those rows in column 3 and again, find rows where the data are equal to the
> row in question, after it has found these rows I need it to look in column 4
> for values, add them up and divide the sum by 3.
>
> I tried to explain that as best I could, and I can go into more detail if it
> helps clarify what I'm after.
>
> But like I said, any help would be great.
>
> Cheers.
>
>
> </quote>
> Quoted from:
> http://r.789695.n4.nabble.com/Dataframe-manipulation-tp4662844.html
More information about the R-help
mailing list