1

I have two data sets, one for student level data and another one for class level data. Student and class level IDs are generated as string values like:

Student data set:

student ID ->141PSDM2L,1420CHY1L,1JNLV36HH,1MNSBXUST,2K7EVS7X6,2N2SC26HL,...

class ID ->XK37HDN,XK37HDN,XK37HDN,3K3EH77,3K3EH77,2K36HN6,...

class level data set:

class ID ->XK37HDN,3K3EH77,2K36HN6,3K3LHSH,3K3LHSY,DK3EH14,DK3EH1H,DK3EH1K,...

In student data set,each class ID is repeated equal to the number of students in the class but in class level data set we only have one code for each class.

How can I convert those ID into integers? considering both student and class level ID.IN other words, I want to have IDs as below (or something similar):

Student data set:

student ID ->1,2,3,4,5,6,...

class ID ->1,1,1,2,2,3,...

class level data set:

class ID ->1,2,3,4,5,6,7,8,...

EDIT: Conversion on student level data is not difficult. The problem arises when I want to convert class level data. Because of the repetition of class IDs in student data set, class IDs take values from 1 to 1533 but doing the same conversion method in class level data produces values from 1 to 896 so I don't know if , for example,class ID of 45 in student level data has the position as class ID 45 in class level data set.

6
  • @Momo Thanks for your prompt reply but as added to the post, I have no problem with conversion in each file. I'm looking for a way of matching class IDs in both data sets.
    – Amin
    Sep 17, 2013 at 22:31
  • 1
    Can I ask why you want to convert these to integers? I suspect you may not need to, although you may need get the levels the same on the two sets of factors. merge() may also be useful if you want to get Class information alongside the Student information. Sep 17, 2013 at 22:51
  • Although this question has tags that would make it look statistical in nature, I cannot see any statistical problem posed in the question. I would suggest asking this question on stack overflow.
    – user2005253
    Sep 18, 2013 at 2:09
  • @GavinSimpson for two reasons,a) to learn how to convert those structured IDs and b) I think that having integers as IDs can reduce the possibility of misreading the data.
    – Amin
    Sep 18, 2013 at 2:14
  • 1
    @Amin please don't post on Stack Overflow. At the very least, if suitable for Stack Overflow, the moderators here can migrate the Q&A in its entirety, including the comment thread and any answers. Sep 18, 2013 at 2:40

1 Answer 1

2

Assuming that your studentID and classID are factors, I would use the fact that internally these are stored numerically. Hence if you can get the levels the same on both factors (i.e. in same order, and such that identical(levels(f1), levels(f2)) == TRUE), then you can simply coerce to integers.

I was thinking something along the lines of:

## dummy data first
set.seed(1)
df1 <- data.frame(f1 = sample(letters, 100, replace = TRUE),
                  f2 = sample(LETTERS, 100, replace = TRUE,
                  prob = rep(c(0.25, 0.75), length = 26)))
df2 <- with(df1, data.frame(f2 = sample(factor(unique(f2),
                            levels = sample(unique(f2)))),
                            vals = rnorm(length(unique(f2)))))

Note the levels of the factors are not identical even though there is a match between the data (given the way I generated them)

> identical(with(df1, levels(f2)), with(df2, levels(f2)))
[1] FALSE

Now make the levels identical, here I just take the union in case there are some values in one factor and not the other, and vice versa.

## make levels identical
levs <- sort(union(with(df1, levels(f2)), with(df2, levels(f2))))
df1 <- transform(df1, f2 = factor(f2, levels = levs))
df2 <- transform(df2, f2 = factor(f2, levels = levs))

> identical(with(df1, levels(f2)), with(df2, levels(f2)))
[1] TRUE

Now record to numeric

## recode as numeric
df1b <- transform(df1, f2int = as.numeric(f2))
df2b <- transform(df2, f2int = as.numeric(f2))

> head(df1b)
  f1 f2 f2int
1  g  B     2
2  j  D     4
3  o  R    17
4  x  A     1
5  f  F     6
6  x  J    10
> head(df2b)
  f2        vals f2int
1  Z -0.17955653    23
2  U -0.10019074    20
3  N  0.71266631    13
4  J -0.07356440    10
5  B -0.03763417     2
6  X -0.68166048    22

Notice the f1int and f2int values for f2 equal to B or J.

My point in the comments about merge() was if you want to match the tables, you can do the usual database joins using merge(). E.g.:

> head(merge(df1, df2, sort = FALSE))
  f2 f1        vals
1  B  g -0.03763417
2  B  v -0.03763417
3  B  u -0.03763417
4  B  e -0.03763417
5  B  w -0.03763417
6  D  i -0.58889449

which would avoid the potentially error-prone step of getting the levels in order and converting to integers, if this was the ultimate aim.

2
  • thanks for your response.It was helpful. I came across to another problem. I have 580 unique IDs in student data and 680 unique IDs in class data. By using `intersect' I found out that 487 IDs are the same across the two data sets. How can I pick data for each data set with respect to the same IDs across data sets?
    – Amin
    Sep 18, 2013 at 20:10
  • @Amin Ask that as another question. Sep 18, 2013 at 21:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.