[R] Parsing array data
markleeds at verizon.net
markleeds at verizon.net
Tue Aug 12 21:49:29 CEST 2008
Hi: you can do
result<-lapply(agg, function(.df) {
mean(.df$Discharge)
})
this will give you the mean of the Discharge column in the various
dataframes. aggregate s used more when you want to do calculations
on various subset by another variable. if you want to do that, you can.
like say there was an ID variable in addition to Discharge. Then, you
can
do
result<-lapply(agg, function(.df) {
aggregate(.df$Discharge,id=list(.df$ID),mean,na.rm=TRUE)
})
On Tue, Aug 12, 2008 at 2:39 PM, Altaweel, Mark R. wrote:
> Hi,
>
> Great, that does solve most of my problem.
>
> I loaded the files, and did this: a <-agg[[1]]
> meanD=mean(a[['Discharge']]) # returning the mean of the variable
> discharge for the first data element in agg
>
> Now, if I wanted to get the mean or just aggregate the variable
> "Discharge" of the entire array (which is length 3), is there a way to
> do that easily? I think the function aggregate(x,...) might have
> something to do with it, as I saw examples there. However, I have not
> successfully been able to get the data to aggregate.
>
> Thanks again.
>
> Mark
>
>
>
>
> -----Original Message-----
> From: markleeds at verizon.net [mailto:markleeds at verizon.net]
> Sent: Tue 8/12/2008 1:05 PM
> To: Altaweel, Mark R.
> Subject: RE: [R] Parsing array data
> try a<-res[[1]] instead of a<-res[1].
>
> you need to acess the dataframe and , to do that, you need to access
> WHAT'S IN THE FIRST COMPONENT OF THE LIST NOT THE FIRST LIST
> COMPONENT ITSELF. so that's why you need the double brackets. It takes
> time to get one's hand around this list concept and the different
> ways of bracketing but, when you do, it's a powerful mechanism. I also
> recommending staying on this list and watching and trying solutions if
> you want to get into
> R more deeply. I'm trying and I find that's the best way to reach that
> goal. good luck.
>
>
> mark
>
>
> On Tue, Aug 12, 2008 at 1:35 PM, Altaweel, Mark R. wrote:
>
>> Hi,
>>
>> I read in csv files with the following code:
>>
>> res <- vector(mode="list",length=3)
>> for(i in 1: length(res))
>>
>>
>>
>> res[[i]]<-read.csv(file=paste("/Users/markaltaweel/Desktop/Output/HydroDataOutput",i,".csv",sep=""),header=T,sep=",")
>>
>> This allows me to load the data into an array of length 3, with the
>> res array containing my data from the csv files.
>>
>> I would like to parse the data in the res array so that I can access
>> my specific data columns. For instance, I have field names called
>> 'Discharge,' 'Volume,' and 'Id'. I would like to aggregate these
>> fields for all the loaded files in the array so that I can perform
>> some basic comparisons of distributions, etc.
>> Does anyone have any example code that would allow me to parse the
>> array so that I can extract the column names and perform some data
>> aggregation and basic stats. Currently, even if I do the following
>> code I get a Null response:
>>
>> a=res[1]
>> a[['Discharge']] # this returns [[1]] NULL
>>
>> Please let me know if there is a solution to this or what am I doing
>> wrong.
>>
>> Thank you in advance.
>>
>> Mark
>>
>> ______________________________________________
>> 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