[R] How to add specific column to data.set?
R. Michael Weylandt
michael.weylandt at gmail.com
Wed Apr 18 09:04:26 CEST 2012
I'll unpack it bit by bit:
stringsAsFactors = FALSE is a command to tell R how to construct the
data frame: this doesn't apply to you because you already have a data
frame, but I need it to set up my examples. By default, when you give
R data that looks like strings/labels, it wants to convert those to
factors (category data), but this stops that behavior and keeps the
characters. This is considered by many to be a bad default, but it's
so fundamental to the language that it couldn't be changed now without
breaking lots of code. Perhaps something we'll see in an R 3.0 if that
ever happens.
lapply(countries, function(x) x %in% euro) -- function(x) x%in% euro
simply checks for each x which elements are in the european union and
returns logical (True/False) values -- the lapply call goes column by
column (here just the two in my example) because data.frames are
really lists and each column is a list element. This latter point is
somewhat subtle so don't worry too much about it just yet -- I could
have done apply(, 2, ) and it would be just as good.
Reduce -- takes a list of arguments and combines them sequentially
with another function, here `&` (the AND function) -- For instance, a
sum function could be written as Reduce(`+`, inputs) e.g.,
Reduce(`+`, list(1,2,3,4)) = 1 + Reduce(`+`, list(2,3,4)) = 1 + 2 +
Reduce(`+`, list(3,4)) = 3 + 3 + Reduce(`+`, list(4)) = 6 + 4 = 10 --
for this example, it checks all the countries (just two columns) and
ANDs them (per your request)
cbind -- attaches a new column
dput simply converts an object to plain text so it can be emailed
easily -- the console printout doesn't give enough information for us
to reproduce your problem/data, but dput will. It's not specific to
your problem -- just a mailing list / reproducibility hint.
Hope this helps,
Michael
On Wed, Apr 18, 2012 at 2:24 AM, phillip03 <phillipbrigham at hotmail.com> wrote:
> Wow it works :) Thank you SO much!!
> I am very new at R and was thinking if you would explain what these to codes
> do:
>
> countries<-data.frame(country1,country2,stringsAsFactors=FALSE)
>
> # I know this i a vector with both my county lists but what does
> stringsAsFacors= FALSE do ?. What if I wantede a row with 1=TRUE and 0=FALSE
>
> and
>
>
> cbind(countries,EMU=Reduce(`&`, lapply(countries,
> + function(x) x %in% euro)))
>
> # here I columnbind my new defined vector and produce a new one ? What does
> the Reduce and lapply do ?
>
>
> I am also not quite sure of the dput() you wrote michael sorry. I understand
> if you dont have the time to help further :)
>
> Ph
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-add-specific-column-to-data-set-tp4565341p4566822.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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