[R] hi have a question about merging.
Joshua Wiley
jwiley.psych at gmail.com
Thu Dec 9 05:24:14 CET 2010
Hi,
Just as a note, it is preferred that you use plain text rather than
rich text or html emails for this list.
On Wed, Dec 8, 2010 at 7:32 PM, tanzia chaudhury
<lumous_hillary at hotmail.com> wrote:
>
> this is the problem:
> load this R data frame over the internet and save it to your hard drive.
> http://rss.acs.unt.edu/Rdoc/library/twang/data/racepro ling.RData
>
> please show how to save a dataset of males only (the variable male=1) to a new dataframe. Then do the same thing for females (male=0). Then show how to recombine the two datasets to belike the original one except that the female observations come rst, then the male observations. This resultingdataset should be the same size as the original.
Just so we are clear, this seems like homework, so I *am not* going to
give a solution.
> this is what i did so far:
> # loading the R data frame over the internet
> and saving it to your hard drive
>>rp<-url("http://rss.acs.unt.edu/Rdoc/library/twang/data/raceprofiling.RData")
>>load(rp)
>>str(raceprofiling)
Good so far
> #this shows that in the RData file there are
> 5000 obs. of 10 variables.
right
> # For
> the fourth dataset, please show how to save a dataset of males only (the
> variable male=1)
> #to a new #data frame.
>>attach(raceprofiling)
attach()ing data frames is often not the preferred method, and there
is no real need in your case here. One alternative is with(), you can
pull up the documentation by typing: ?with at your console.
>>s<-male[male==1]
Just print "s" to the console and see what it is. I think if you did
that, you would see why what you have done is not working.
>>news<-data.frame(s)
now you turn "s" into a data frame, but it still does not contain all
the information you want.
> #The same thing for females (male=0).
>>r<-male[male==0]
>>newr<-data.frame(r)
> now to merge:
> I tried >merge(newr,news)
> but this does not give the same observation as the original file. I also tried rbind, but this gives me error. Please help
Correct, this does not give the same observations as the original.
You used the logical comparison "male == 1", which is okay here, but
what data did you select from? Did you select from the entire data
frame or did you *only* select from the "male" column of data? To put
it another way, you took a variable that only coded gender (male) and
then selected the levels of that variable for a particular gender
(first for males then for females). That is well and good, but you
have so far ignored the rest of the dataset. If you retrace your
steps, but do not attach the dataframe, I suspect you might get the
correct solution:
raceprofiling[raceprofiling$male == 1, "male"]
(which is what you would have to do) looks pretty silly and might lead
you to the correct method. It would be worth reading through:
?"[" # the extraction operator
pay particular note to the fact that a data frame is kind of like a 2
dimensional table, so you can select either rows or columns, that is:
[rows, columns]. If you leave one or the other blank, it selects all
for that dimension.
Cheers and good luck,
Josh
> [[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.
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
More information about the R-help
mailing list