Convert rows in a data frame

7 views
Skip to first unread message

Ben Bimber

unread,
Apr 28, 2011, 2:47:38 PM4/28/11
to maduser
Hello,

I have a dataframe of pedigree data (Id, mother, father). For some
subjects, we can only identify 1 parent. I'd like to convert the
mother and father values to NA on any row lacking a value for either
mother or father. What's the most efficient way to approach that?
Here is sample data:

#sample data
id <- c('patient1', 'patient2', 'patient3')
m <- c('mother1', NA, 'mother3')
f <- c('father1', 'father2', NA)

df1 <- data.frame(id=id, mother=m, father=f);

based on this data, patient1 should retain both mother/father.
Patient2 would have father converted to NA because it lacks a mother.
Patient3 would have the mother converted to NA b/c it lacks a father.

Thanks for any help,
Ben

Douglas Bates

unread,
Apr 28, 2011, 3:23:25 PM4/28/11
to mad...@googlegroups.com

I'm not quite sure in what sense you want those values "converted".
They are already NA's

> #sample data
> id <- c('patient1', 'patient2', 'patient3')
> m <- c('mother1', NA, 'mother3')
> f <- c('father1', 'father2', NA)
> df1 <- data.frame(id=id, mother=m, father=f);

> str(df1)
'data.frame': 3 obs. of 3 variables:
$ id : Factor w/ 3 levels "patient1","patient2",..: 1 2 3
$ mother: Factor w/ 2 levels "mother1","mother3": 1 NA 2
$ father: Factor w/ 2 levels "father1","father2": 1 2 NA

Notice that there are only two levels for the mother and father variables.

Ben Bimber

unread,
Apr 28, 2011, 4:15:17 PM4/28/11
to mad...@googlegroups.com
Sorry, could have explained better. I'm feeding a big dataframe into
the kinship pedigree package. if a subject doesnt have 2 parents, the
pedigree function throws an error. each subject must either have 0 or
2 parents.

i'd like to find any instance where a subject has 1 parent listed, and
then convert both parents to NA for that subject. the reason you
would retain subjects lacking parent info is b/c they could be a
parent.

thanks,
ben

Douglas Bates

unread,
Apr 28, 2011, 4:20:27 PM4/28/11
to mad...@googlegroups.com
On Thu, Apr 28, 2011 at 3:15 PM, Ben Bimber <bbi...@gmail.com> wrote:
> Sorry, could have explained better.  I'm feeding a big dataframe into
> the kinship pedigree package.  if a subject doesnt have 2 parents, the
> pedigree function throws an error.  each subject must either have 0 or
> 2 parents.

> i'd like to find any instance where a subject has 1 parent listed, and
> then convert both parents to NA for that subject.  the reason you
> would retain subjects lacking parent info is b/c they could be a
> parent.

And I didn't read your message carefully enough to understand what you
were asking.

You need to use is.na to test for NA's in one column and propagate
them to the other.

> df1
id mother father
1 patient1 mother1 father1
2 patient2 <NA> father2
3 patient3 mother3 <NA>
> df1$mother[is.na(df1$father)] <- NA
> df1$father[is.na(df1$mother)] <- NA
> df1
id mother father
1 patient1 mother1 father1
2 patient2 <NA> <NA>
3 patient3 <NA> <NA>

Reply all
Reply to author
Forward
0 new messages