0

I have a data frame with IDs (names), as shown below:

head(ped1)
 V1       V2       V3
 31701    31272    25526
 31702    31272    26002
 31703    31272    25861
 31704    31272    24324
 31705    31272    31413
 31706    31272    31701

I need to attribute for each element of the first column new IDs (sequential numbers). Furthermore, the ID in column V1 can show up in V2 and V3.

So the ID has to match in other columns as well, like this:

     head(ped1)
     V1       V2       V3
     1        7        8
     2        7        9
     3        7        10
     4        7        11
     5        7        12
     6        7        1

How do I attribute new IDs?

lmo
  • 37,904
  • 9
  • 56
  • 69
  • Please create a faithful representation of your problem with dummy column names for ID,progeny,etc. and highlight one example of attribution, see [how to post a great reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more details – Silence Dogood Mar 20 '17 at 15:52
  • Sorry, I am new here. Is it clearer? – Baltasar Neto Mar 20 '17 at 16:52

1 Answers1

0

Here is a method in base R using match and sapply on the unique vector of the ids obtained with unlist.

# get IDs as a vector
ids <- unique(unlist(df))

# produce data.frame with new ids by feeding each variable to match, using ids as table
data.frame(lapply(df, match, table=ids))
  V1 V2 V3
1  1  7  8
2  2  7  9
3  3  7 10
4  4  7 11
5  5  7 12
6  6  7  1

data

df <- read.table(header=TRUE, text="V1       V2       V3
 31701    31272    25526
31702    31272    26002
31703    31272    25861
31704    31272    24324
31705    31272    31413
31706    31272    31701")
lmo
  • 37,904
  • 9
  • 56
  • 69