Quantcast

Apply() on columns

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Apply() on columns

faelsendoorn
This post was updated on .
Hi, I have some trouble with the following: I have a table of 3 rows and 6 columns. The columns 1,2,3 have information about the number of employees. The columns 4,5,6 have information about the number of working hours. Each row, is corresponding with a week. I am interested in the average working hours per employee. I think that I can show this by making some boxplots etc. But how can I calculate the average working hours per employee? Think of a table like this: V1 V2 V3 V4 V5 V6 1 2 3 4 12 20 14 2 5 9 4 30 23 30 3 0 0 4 0 0 15 By using apply() I can do some operations by row or column, but I do not see how I can do that with row column to calculate the average. Yours, Frans Elsendoorn
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Apply() on columns

Adams, Jean
Frans,

I'm not sure I understand what you're after.

I suggest that you share a small example data set, using dput().
Then give an example of what you want the output to look like.

Jean


faelsendoorn <[hidden email]> wrote on 06/15/2012 03:09:41 AM:

> Hi,
>
> I have some trouble with the following: I have a table of 7 rows and
> 6columns. The columns 1,2,3 have information about the number of
employees.
> The columns 4,5,6 have information about the number of working hours.
Each
> row, is corresponding with a week.
> My goal is to make a boxplot, histogram etc. of the columns 4, 5 and 6
> (thus, the data of the number of working hours). How can I select by
using
> the function Apply the weeks i needed for fullfulling my goal?
>
> I already tried something with environment variables but that is a hard
> stop.  I think, when I can select the columns I wanted with the
> corresponding rows, that it would not be a problem making a histogram
etc.
>
> Yours,
>
> Frans Elsendoorn

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Apply() on columns

Michael Weylandt
Just to take a stab at it, I'd suggest you don't actually need apply()
and could simply get what you need with

hist(DATA[,4:6])

if your data is as described.

Michael

On Fri, Jun 15, 2012 at 7:23 AM, Jean V Adams <[hidden email]> wrote:

> Frans,
>
> I'm not sure I understand what you're after.
>
> I suggest that you share a small example data set, using dput().
> Then give an example of what you want the output to look like.
>
> Jean
>
>
> faelsendoorn <[hidden email]> wrote on 06/15/2012 03:09:41 AM:
>
>> Hi,
>>
>> I have some trouble with the following: I have a table of 7 rows and
>> 6columns. The columns 1,2,3 have information about the number of
> employees.
>> The columns 4,5,6 have information about the number of working hours.
> Each
>> row, is corresponding with a week.
>> My goal is to make a boxplot, histogram etc. of the columns 4, 5 and 6
>> (thus, the data of the number of working hours). How can I select by
> using
>> the function Apply the weeks i needed for fullfulling my goal?
>>
>> I already tried something with environment variables but that is a hard
>> stop.  I think, when I can select the columns I wanted with the
>> corresponding rows, that it would not be a problem making a histogram
> etc.
>>
>> Yours,
>>
>> Frans Elsendoorn
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Apply() on columns

Peter Ehlers
In reply to this post by faelsendoorn
On 2012-06-15 01:09, faelsendoorn wrote:
> Hi,
>
> I have some trouble with the following: I have a table of 7 rows and
> 6columns. The columns 1,2,3 have information about the number of employees.
> The columns 4,5,6 have information about the number of working hours. Each
> row, is corresponding with a week.
> My goal is to make a boxplot, histogram etc. of the columns 4, 5 and 6

I don't know what the "etc" is, but I strongly suggest that you NOT
make boxplots or histograms for data consisting of 7 values.
Consider stripchart().

Peter Ehlers

> (thus, the data of the number of working hours). How can I select by using
> the function Apply the weeks i needed for fullfulling my goal?
>
> I already tried something with environment variables but that is a hard
> stop.  I think, when I can select the columns I wanted with the
> corresponding rows, that it would not be a problem making a histogram etc.
>
> Yours,
>
> Frans Elsendoorn
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Apply-on-columns-tp4633468.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Apply() on columns

faelsendoorn
Hi,

It wasn't my purpose to quote the assignment here, but since my example is crappy I will.
So I'am a manager of a supermarket and interested in the average working hours per employee.
At this link you can find the dataset.
The dataset contains the total number of employees (first 5 columns) together with the number of worked hours (last 5 columns) of the evening filling team of the supermarket over the period 2 Januari 1992 until 2 January 1993 (each row contains the data of one week). Summerize the data as informative as possible for the manager; use graphical and numerical summaries for this. For this you may use the function apply. To load these data you may prefer to use the R-function read.table instead of the function scan.

By graphical summary I thougt of: boxplot, histogram, qqplot
By numerical summary I thougt of: 5pointssummary.

How can I use apply (because I must use that function) in order to make those summaries?

Yours

Frans Elsendoorn
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Apply() on columns

John Kane
Maybe I'm just slow today but what are the columns? I had a look at the dataset and it has no headers so reading it into R just supplies us with something like :  dput(head(sm))

sup  <-  structure(list(V1 = c(7L, 7L, 7L, 7L, 9L, 7L), V2 = c(6L, 8L,
8L, 9L, 9L, 6L), V3 = c(8L, 8L, 7L, 9L, 6L, 8L), V4 = c(8L, 8L,
10L, 8L, 7L, 7L), V5 = c(8L, 9L, 10L, 8L, 8L, 7L), V6 = c(30.25,
26.75, 28.75, 33.25, 32.25, 27.5), V7 = c(20.25, 33, 33, 32.75,
35.25, 25.5), V8 = c(29.75, 27.75, 25.75, 29, 23, 26.75), V9 = c(28.25,
30, 36.75, 26.25, 24.25, 30.75), V10 = c(24.5, 43, 41.25, 33.5,
34.75, 35.25)), .Names = c("V1", "V2", "V3", "V4", "V5", "V6",
"V7", "V8", "V9", "V10"), row.names = c(NA, 6L), class = "data.frame")

Presumably V1 is related to V6 somehow?

If so what is one expected to do with the data?


John Kane
Kingston ON Canada


> -----Original Message-----
> From: [hidden email]
> Sent: Tue, 19 Jun 2012 01:29:26 -0700 (PDT)
> To: [hidden email]
> Subject: Re: [R] Apply() on columns
>
> Hi,
>
> It wasn't my purpose to quote the assignment here, but since my example
> is
> crappy I will.
> So I'am a manager of a supermarket and interested in the average working
> hours per employee.
> At  http://www.mijnbestand.nl/Bestand-6Z8PB7VNSUHZ.txt this link  you can
> find the dataset.
> The dataset contains the total number of employees (first 5 columns)
> together with the number of worked hours (last 5 columns) of the evening
> filling team of the supermarket over the period 2 Januari 1992 until 2
> January 1993 (each row contains the data of one week). Summerize the data
> as
> informative as possible for the manager; use graphical and numerical
> summaries for this. For this you may use the function apply. To load
> these
> data you may prefer to use the R-function read.table instead of the
> function
> scan.
>
> By graphical summary I thougt of: boxplot, histogram, qqplot
> By numerical summary I thougt of: 5pointssummary.
>
> How can I use apply (because I must use that function) in order to make
> those summaries?
>
> Yours
>
> Frans Elsendoorn
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Apply-on-columns-tp4633468p4633813.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Apply() on columns

faelsendoorn
Hi,

Yes, the columns are related: V1 is related to V6, V2 is related to V7 and so on. The columns V1,V2,V3,V4,V5 contains the number of employees (in a filling team). The columns V6,V7,V8,V9,V10 contains the number of worked hours of the filling team.
What I am interested in is the average working hours per employee. Therefore I have go give some graphical and numerical summaries to present the average working hours per employee.  The function that I must use is apply().

How can I present with that function a graphical and numerical summarie of the average working hours per employee? [some example and explanation would be respected]

Yours,

FA Elsendoorn
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Apply() on columns

John Kane
Okay, I think you need to read up a bit on how to use R.  

Have a look at ?apply.  It has nothing to do with graphing data. It is a way to summarize data.

What I think you want to do is to select the first 4 columns and then use apply() wih sum to get the total number of worlers  and then use apply(() to get the sum of the hours.

You will get two vectors workers and hours ,  Then hours/workers gives you the mean hours worked by each team.  Since this looks like a homework assignment I will leave the actual programing to you.

To graph something you need to select the data you want and then give the command you want

For example if we call the data.frame 'sp' then  boxplot(sp$V6)  gives you a boxplot of V6.
Have a look at ?boxplot for more information on how to make the plot look better by adding titles etc.

Also try with(sp, boxplot(V6,V7,V8, V9, V10 ))  for a grouped boxplot




John Kane
Kingston ON Canada


> -----Original Message-----
> From: [hidden email]
> Sent: Tue, 19 Jun 2012 22:25:31 -0700 (PDT)
> To: [hidden email]
> Subject: Re: [R] Apply() on columns
>
> Hi,
>
> Yes, the columns are related: V1 is related to V6, V2 is related to V7
> and
> so on. The columns V1,V2,V3,V4,V5 contains the number of employees (in a
> filling team). The columns V6,V7,V8,V9,V10 contains the number of worked
> hours of the filling team.
> What I am interested in is the average working hours per employee.
> Therefore
> I have go give some graphical and numerical summaries to present the
> average
> working hours per employee.  The function that I must use is apply().
>
> How can I present with that function a graphical and numerical summarie
> of
> the average working hours per employee? [some example and explanation
> would
> be respected]
>
> Yours,
>
> FA Elsendoorn
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Apply-on-columns-tp4633468p4633917.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Apply() on columns

PIKAL Petr
In reply to this post by faelsendoorn
Hi
 
> Hi,
>
> Yes, the columns are related: V1 is related to V6, V2 is related to V7
and
> so on. The columns V1,V2,V3,V4,V5 contains the number of employees (in a
> filling team). The columns V6,V7,V8,V9,V10 contains the number of worked
> hours of the filling team.

You shall rather include your data.frame to email by dput instead of just
vaguely describing it. If it is too big use only part of it.


> What I am interested in is the average working hours per employee.
Therefore
> I have go give some graphical and numerical summaries to present the
average
> working hours per employee.  The function that I must use is apply().

Why do you need to use apply? Is it a homework? There is no homework
policy on this list.

I would use some kind of ?aggregate construction.

Regards
Petr

>
> How can I present with that function a graphical and numerical summarie
of
> the average working hours per employee? [some example and explanation
would

> be respected]
>
> Yours,
>
> FA Elsendoorn
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Apply-on-
> columns-tp4633468p4633917.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Apply() on columns

faelsendoorn
I do now know how to navigate through the table.
But my question is, what kind of graphical and numerical summaries can I make with keeping in mind that I am interested in the average working hour per employee?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Apply() on columns

PIKAL Petr
>
> I do now know how to navigate through the table.
> But my question is, what kind of graphical and numerical summaries can I
> make with keeping in mind that I am interested in the average working
hour
> per employee?

Rather vague question without data or code, so rather vague answer.

For numerical values summarised according to some factor see

?aggregate
?by
?tapply

For graphical summary maybe

?dotplot
?boxplot
or maybe you could try package ggplot2

Regards
Petr

>
> --
> View this message in context: http://r.789695.n4.nabble.com/Apply-on-
> columns-tp4633468p4634411.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Apply() on columns

Rui Barradas
In reply to this post by faelsendoorn
Hello,

It's not that complicated.



f <- function(index, dat, offset=5){
        x <- data.frame(Emp=dat[, index], Hours=dat[, index + offset])
        aggregate(Hours~Emp, data=x, sum)
}

sp <- read.table("supermarkt.txt")

lapply(1:5, f, sp)


Also, please provide context. (Quote the post you are reffering to.)

Hope this helps,

Rui Barradas

Em 25-06-2012 11:43, faelsendoorn escreveu:

> I do now know how to navigate through the table.
> But my question is, what kind of graphical and numerical summaries can I
> make with keeping in mind that I am interested in the average working hour
> per employee?
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Apply-on-columns-tp4633468p4634411.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Apply() on columns

Rui Barradas
Hello, again.

Sorry, I've used 'sum' when it should be 'mean'.

Rui Barradas

Em 25-06-2012 12:49, Rui Barradas escreveu:

> Hello,
>
> It's not that complicated.
>
>
>
> f <- function(index, dat, offset=5){
>      x <- data.frame(Emp=dat[, index], Hours=dat[, index + offset])
>      aggregate(Hours~Emp, data=x, sum)
> }
>
> sp <- read.table("supermarkt.txt")
>
> lapply(1:5, f, sp)
>
>
> Also, please provide context. (Quote the post you are reffering to.)
>
> Hope this helps,
>
> Rui Barradas
>
> Em 25-06-2012 11:43, faelsendoorn escreveu:
>> I do now know how to navigate through the table.
>> But my question is, what kind of graphical and numerical summaries can I
>> make with keeping in mind that I am interested in the average working
>> hour
>> per employee?
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Apply-on-columns-tp4633468p4634411.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Loading...