kevin123
2012-Mar-05 19:56 UTC
[R] I want to fill a column in with 0 or 1 depending on another columns input
This is a portion of my data frame MemberID Specialty Surgery Internal 1 42286978 Surgery NA NA 2 97903248 Internal NA NA 3 2759427 Internal NA NA 4 73570559 Surgery NA NA There are four columns. In the Specialty column there are two options Surgery/Internal, These options have there own columns. I want to fill a column in with 0 or 1 depending on another columns input I am finding it hard to figure out how to do the following: MemberID Specialty Surgery Internal 1 42286978 Surgery 1 0 2 97903248 Internal 0 1 3 2759427 Internal 0 1 4 73570559 Surgery 1 0 I would greatly appreciate any help regards, Kevin -- View this message in context: http://r.789695.n4.nabble.com/I-want-to-fill-a-column-in-with-0-or-1-depending-on-another-columns-input-tp4447446p4447446.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2012-Mar-06 00:40 UTC
[R] I want to fill a column in with 0 or 1 depending on another columns input
On Mar 5, 2012, at 2:56 PM, kevin123 wrote:> > This is a portion of my data frame > > MemberID Specialty Surgery Internal > 1 42286978 Surgery NA NA > 2 97903248 Internal NA NA > 3 2759427 Internal NA NA > 4 73570559 Surgery NA NA > > There are four columns. In the Specialty column there are two options > Surgery/Internal, These options have there own columns. > > I want to fill a column in with 0 or 1 depending on another columns > input > > I am finding it hard to figure out how to do the following: > > MemberID Specialty Surgery Internal > 1 42286978 Surgery 1 0 > 2 97903248 Internal 0 1 > 3 2759427 Internal 0 1 > 4 73570559 Surgery 1 0You (user1250865) have already been given THREE answers on StackOverflow. Crossposting is deprecated. (Unless of course this is a homework problem and both kevin123 and user1250865 are in the same class. Then ... homework is also deprecated. -- David Winsemius, MD West Hartford, CT
Rui Barradas
2012-Mar-06 00:42 UTC
[R] I want to fill a column in with 0 or 1 depending on another columns input
Hello, kevin123 wrote> > This is a portion of my data frame > > MemberID Specialty Surgery Internal > 1 42286978 Surgery NA NA > 2 97903248 Internal NA NA > 3 2759427 Internal NA NA > 4 73570559 Surgery NA NA > > There are four columns. In the Specialty column there are two options > Surgery/Internal, These options have there own columns. > > I want to fill a column in with 0 or 1 depending on another columns input > > I am finding it hard to figure out how to do the following: > > MemberID Specialty Surgery Internal > 1 42286978 Surgery 1 0 > 2 97903248 Internal 0 1 > 3 2759427 Internal 0 1 > 4 73570559 Surgery 1 0 > > I would greatly appreciate any help > > regards, > > Kevin >Try text <- "MemberID Specialty Surgery Internal 1 42286978 Surgery NA NA 2 97903248 Internal NA NA 3 2759427 Internal NA NA 4 73570559 Surgery NA NA" x <- read.table(text=text) x # Make a logical vector surg <- x$Specialty == "Surgery" # use it as integer, there are # only two possibilities x$Surgery <- as.integer(surg) x$Internal <- as.integer(!surg) x Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/I-want-to-fill-a-column-in-with-0-or-1-depending-on-another-columns-input-tp4447446p4448296.html Sent from the R help mailing list archive at Nabble.com.