Entering Data Files

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Entering Data Files

David Arnold
Hi,

Entering data from a given file is the hardest part of learning R for me. For example, I have this datafile from Moore's text:

Live Age Count
Parents Age19 324
Another Age19 37
OwnPlace Age19 116
Group Age19 58
Other Age19 5
Parents Age20 378
Another Age20 47
OwnPlace Age20 279
Group Age20 60
Other Age20 2
Parents Age21 337
Another Age21 40
OwnPlace Age21 372
Group Age21 49
Other Age21 3
Parents Age22 318
Another Age22 38
OwnPlace Age22 487
Group Age22 25
Other Age22 9

I can manually enter the numbers and create my table and barplot.

x <- matrix(c(324, 378, 337, 318, 37, 47, 40, 38, 116, 279, 372, 487, 58, 60, 49, 25, 5, 2, 3, 9), c(5,4),byrow=T)
rownames(x)=c("Parents","Another","OwnPlace","Group","Other")
colnames(x)=c("Age19","Age20","Age21","Age22")
x <- as.table(x)

barplot(x,beside=T)

But it would be nice to be able to read the data from the file, arrange it using R, then plot, if that is possible. Any suggestions?

Thanks

David

Reply | Threaded
Open this post in threaded view
|

Re: Entering Data Files

Yasir Kaheil
type ?read.table in R
Yasir Kaheil
Reply | Threaded
Open this post in threaded view
|

Re: Entering Data Files

Rui Barradas
In reply to this post by David Arnold
Hello,

Install package 'reshape2' and try the following.


#install.packages('reshape2')
library(reshape2)

d <- read.table(text="
Live    Age    Count
Parents    Age19    324
Another    Age19    37
OwnPlace    Age19    116
Group    Age19    58
Other    Age19    5
Parents    Age20    378
Another    Age20    47
OwnPlace    Age20    279
Group    Age20    60
Other    Age20    2
Parents    Age21    337
Another    Age21    40
OwnPlace    Age21    372
Group    Age21    49
Other    Age21    3
Parents    Age22    318
Another    Age22    38
OwnPlace    Age22    487
Group    Age22    25
Other    Age22    9
", header=TRUE, stringsAsFactors=FALSE)
d

dmelt <- melt(d, id.vars=c("Live", "Age"), measure.vars="Count")
dcast(Live ~ Age, data = dmelt)


Hope this helps,

Rui Barradas

Em 18-07-2012 21:35, darnold escreveu:

> Hi,
>
> Entering data from a given file is the hardest part of learning R for me.
> For example, I have this datafile from Moore's text:
>
> Live Age Count
> Parents Age19 324
> Another Age19 37
> OwnPlace Age19 116
> Group Age19 58
> Other Age19 5
> Parents Age20 378
> Another Age20 47
> OwnPlace Age20 279
> Group Age20 60
> Other Age20 2
> Parents Age21 337
> Another Age21 40
> OwnPlace Age21 372
> Group Age21 49
> Other Age21 3
> Parents Age22 318
> Another Age22 38
> OwnPlace Age22 487
> Group Age22 25
> Other Age22 9
>
> I can manually enter the numbers and create my table and barplot.
>
> x <- matrix(c(324, 378, 337, 318, 37, 47, 40, 38, 116, 279, 372, 487, 58,
> 60, 49, 25, 5, 2, 3, 9), c(5,4),byrow=T)
> rownames(x)=c("Parents","Another","OwnPlace","Group","Other")
> colnames(x)=c("Age19","Age20","Age21","Age22")
> x <- as.table(x)
>
> barplot(x,beside=T)
>
> But it would be nice to be able to read the data from the file, arrange it
> using R, then plot, if that is possible. Any suggestions?
>
> Thanks
>
> David
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Entering-Data-Files-tp4636943.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
|

Re: Entering Data Files

Rui Barradas
Hello,

Sorry, I forgot the barplot.


dmelt <- melt(d, id.vars=c("Live", "Age"), measure.vars="Count")
wide <- dcast(Live ~ Age, data = dmelt)

col <- rainbow(4)
barplot(as.matrix(wide[, 2:5]), beside=T, col=col)
legend("topleft", wide[, 1], fill=col)


Hope this helps,

Rui Barradas

Em 18-07-2012 22:19, Rui Barradas escreveu:

> Hello,
>
> Install package 'reshape2' and try the following.
>
>
> #install.packages('reshape2')
> library(reshape2)
>
> d <- read.table(text="
> Live    Age    Count
> Parents    Age19    324
> Another    Age19    37
> OwnPlace    Age19    116
> Group    Age19    58
> Other    Age19    5
> Parents    Age20    378
> Another    Age20    47
> OwnPlace    Age20    279
> Group    Age20    60
> Other    Age20    2
> Parents    Age21    337
> Another    Age21    40
> OwnPlace    Age21    372
> Group    Age21    49
> Other    Age21    3
> Parents    Age22    318
> Another    Age22    38
> OwnPlace    Age22    487
> Group    Age22    25
> Other    Age22    9
> ", header=TRUE, stringsAsFactors=FALSE)
> d
>
> dmelt <- melt(d, id.vars=c("Live", "Age"), measure.vars="Count")
> dcast(Live ~ Age, data = dmelt)
>
>
> Hope this helps,
>
> Rui Barradas
>
> Em 18-07-2012 21:35, darnold escreveu:
>> Hi,
>>
>> Entering data from a given file is the hardest part of learning R for me.
>> For example, I have this datafile from Moore's text:
>>
>> Live    Age    Count
>> Parents    Age19    324
>> Another    Age19    37
>> OwnPlace    Age19    116
>> Group    Age19    58
>> Other    Age19    5
>> Parents    Age20    378
>> Another    Age20    47
>> OwnPlace    Age20    279
>> Group    Age20    60
>> Other    Age20    2
>> Parents    Age21    337
>> Another    Age21    40
>> OwnPlace    Age21    372
>> Group    Age21    49
>> Other    Age21    3
>> Parents    Age22    318
>> Another    Age22    38
>> OwnPlace    Age22    487
>> Group    Age22    25
>> Other    Age22    9
>>
>> I can manually enter the numbers and create my table and barplot.
>>
>> x <- matrix(c(324, 378, 337, 318, 37, 47, 40, 38, 116, 279, 372, 487, 58,
>> 60, 49, 25, 5, 2, 3, 9), c(5,4),byrow=T)
>> rownames(x)=c("Parents","Another","OwnPlace","Group","Other")
>> colnames(x)=c("Age19","Age20","Age21","Age22")
>> x <- as.table(x)
>>
>> barplot(x,beside=T)
>>
>> But it would be nice to be able to read the data from the file,
>> arrange it
>> using R, then plot, if that is possible. Any suggestions?
>>
>> Thanks
>>
>> David
>>
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Entering-Data-Files-tp4636943.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.
Reply | Threaded
Open this post in threaded view
|

Re: Entering Data Files

glsnow
In reply to this post by David Arnold
For reading data into R you should start with
http://cran.r-project.org/doc/manuals/R-data.html (or the local copy
that was installed with R on your machine).

For the example above the read.table function should be fine.  If you
want to change the shape of the resulting data frame then look at the
xtabs function, the reshape function or the reshape2 package.

On Wed, Jul 18, 2012 at 2:35 PM, darnold <[hidden email]> wrote:

> Hi,
>
> Entering data from a given file is the hardest part of learning R for me.
> For example, I have this datafile from Moore's text:
>
> Live    Age     Count
> Parents Age19   324
> Another Age19   37
> OwnPlace        Age19   116
> Group   Age19   58
> Other   Age19   5
> Parents Age20   378
> Another Age20   47
> OwnPlace        Age20   279
> Group   Age20   60
> Other   Age20   2
> Parents Age21   337
> Another Age21   40
> OwnPlace        Age21   372
> Group   Age21   49
> Other   Age21   3
> Parents Age22   318
> Another Age22   38
> OwnPlace        Age22   487
> Group   Age22   25
> Other   Age22   9
>
> I can manually enter the numbers and create my table and barplot.
>
> x <- matrix(c(324, 378, 337, 318, 37, 47, 40, 38, 116, 279, 372, 487, 58,
> 60, 49, 25, 5, 2, 3, 9), c(5,4),byrow=T)
> rownames(x)=c("Parents","Another","OwnPlace","Group","Other")
> colnames(x)=c("Age19","Age20","Age21","Age22")
> x <- as.table(x)
>
> barplot(x,beside=T)
>
> But it would be nice to be able to read the data from the file, arrange it
> using R, then plot, if that is possible. Any suggestions?
>
> Thanks
>
> David
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Entering-Data-Files-tp4636943.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.



--
Gregory (Greg) L. Snow Ph.D.
[hidden email]

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

Re: Entering Data Files

David Carlson
In reply to this post by Rui Barradas
You can do this using only xtabs. Using Rui's data.frame, d:

x <- xtabs(Count~Live+Age, d)

barplot(x, beside=T, legend.text=TRUE, args.legend=list(x="topleft"))

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Rui Barradas
> Sent: Wednesday, July 18, 2012 4:24 PM
> To: darnold
> Cc: [hidden email]
> Subject: Re: [R] Entering Data Files
>
> Hello,
>
> Sorry, I forgot the barplot.
>
>
> dmelt <- melt(d, id.vars=c("Live", "Age"), measure.vars="Count")
> wide <- dcast(Live ~ Age, data = dmelt)
>
> col <- rainbow(4)
> barplot(as.matrix(wide[, 2:5]), beside=T, col=col)
> legend("topleft", wide[, 1], fill=col)
>
>
> Hope this helps,
>
> Rui Barradas
>
> Em 18-07-2012 22:19, Rui Barradas escreveu:
> > Hello,
> >
> > Install package 'reshape2' and try the following.
> >
> >
> > #install.packages('reshape2')
> > library(reshape2)
> >
> > d <- read.table(text="
> > Live    Age    Count
> > Parents    Age19    324
> > Another    Age19    37
> > OwnPlace    Age19    116
> > Group    Age19    58
> > Other    Age19    5
> > Parents    Age20    378
> > Another    Age20    47
> > OwnPlace    Age20    279
> > Group    Age20    60
> > Other    Age20    2
> > Parents    Age21    337
> > Another    Age21    40
> > OwnPlace    Age21    372
> > Group    Age21    49
> > Other    Age21    3
> > Parents    Age22    318
> > Another    Age22    38
> > OwnPlace    Age22    487
> > Group    Age22    25
> > Other    Age22    9
> > ", header=TRUE, stringsAsFactors=FALSE)
> > d
> >
> > dmelt <- melt(d, id.vars=c("Live", "Age"), measure.vars="Count")
> > dcast(Live ~ Age, data = dmelt)
> >
> >
> > Hope this helps,
> >
> > Rui Barradas
> >
> > Em 18-07-2012 21:35, darnold escreveu:
> >> Hi,
> >>
> >> Entering data from a given file is the hardest part of learning R
> for me.
> >> For example, I have this datafile from Moore's text:
> >>
> >> Live    Age    Count
> >> Parents    Age19    324
> >> Another    Age19    37
> >> OwnPlace    Age19    116
> >> Group    Age19    58
> >> Other    Age19    5
> >> Parents    Age20    378
> >> Another    Age20    47
> >> OwnPlace    Age20    279
> >> Group    Age20    60
> >> Other    Age20    2
> >> Parents    Age21    337
> >> Another    Age21    40
> >> OwnPlace    Age21    372
> >> Group    Age21    49
> >> Other    Age21    3
> >> Parents    Age22    318
> >> Another    Age22    38
> >> OwnPlace    Age22    487
> >> Group    Age22    25
> >> Other    Age22    9
> >>
> >> I can manually enter the numbers and create my table and barplot.
> >>
> >> x <- matrix(c(324, 378, 337, 318, 37, 47, 40, 38, 116, 279, 372,
> 487, 58,
> >> 60, 49, 25, 5, 2, 3, 9), c(5,4),byrow=T)
> >> rownames(x)=c("Parents","Another","OwnPlace","Group","Other")
> >> colnames(x)=c("Age19","Age20","Age21","Age22")
> >> x <- as.table(x)
> >>
> >> barplot(x,beside=T)
> >>
> >> But it would be nice to be able to read the data from the file,
> >> arrange it
> >> using R, then plot, if that is possible. Any suggestions?
> >>
> >> Thanks
> >>
> >> David
> >>
> >>
> >>
> >> --
> >> View this message in context:
> >> http://r.789695.n4.nabble.com/Entering-Data-Files-tp4636943.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.

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

Re: Entering Data Files

John Kane
In reply to this post by David Arnold

?read.table
Using my normal file path and th data as you supplied it.
x  <-   read.csv("/home/john/rdata/ages.csv", sep = " ", header = TRUE)

Change the file path to whatever yours is.

Example might be
x  <-  read.csv("C:/mydata/ages.csv", sep = " ", header = TRUE)
in Windows.

Note that you can do either Linux style /  or use Windows \ but if so you must escape them so the path would be "C:\\mydata\\ages.csv".

Hope this helps.  


John Kane
Kingston ON Canada


> -----Original Message-----
> From: [hidden email]
> Sent: Wed, 18 Jul 2012 13:35:00 -0700 (PDT)
> To: [hidden email]
> Subject: [R] Entering Data Files
>
> Hi,
>
> Entering data from a given file is the hardest part of learning R for me.
> For example, I have this datafile from Moore's text:
>
> Live Age Count
> Parents Age19 324
> Another Age19 37
> OwnPlace Age19 116
> Group Age19 58
> Other Age19 5
> Parents Age20 378
> Another Age20 47
> OwnPlace Age20 279
> Group Age20 60
> Other Age20 2
> Parents Age21 337
> Another Age21 40
> OwnPlace Age21 372
> Group Age21 49
> Other Age21 3
> Parents Age22 318
> Another Age22 38
> OwnPlace Age22 487
> Group Age22 25
> Other Age22 9
>
> I can manually enter the numbers and create my table and barplot.
>
> x <- matrix(c(324, 378, 337, 318, 37, 47, 40, 38, 116, 279, 372, 487, 58,
> 60, 49, 25, 5, 2, 3, 9), c(5,4),byrow=T)
> rownames(x)=c("Parents","Another","OwnPlace","Group","Other")
> colnames(x)=c("Age19","Age20","Age21","Age22")
> x <- as.table(x)
>
> barplot(x,beside=T)
>
> But it would be nice to be able to read the data from the file, arrange
> it
> using R, then plot, if that is possible. Any suggestions?
>
> Thanks
>
> David
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Entering-Data-Files-tp4636943.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.

____________________________________________________________
Receive Notifications of Incoming Messages
Easily monitor multiple email accounts & access them with a click.
Visit http://www.inbox.com/notifier and check it out!

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