How to link classification file to identifier in a frame?

11 Views Asked by At

I have two files, one with classification codes with corresponding names, and one original data frame that contains only the classification codes.

I want to add to the data frame the corresponding name to each identifier as listed in the classification codes file (which contains much more unique identifiers than the original data file).

How can I do this?

I have tried to use a for-loop as follows,

for (i in 1:length(data$SCC)) {
  for (j in 1:length(classification$SCC)){
    if (as.numeric(data$SCC[i]) == as.numeric.factor(classification$SCC[j])) {
      data$Short.Name[i] <- classification$Short.Name[j]; 
  }
 }
}

Where SCC is the variable that contains the identifiers (e.g. "1001004"), which is the same in the 'classification' frame (classification$SCC).

In 'classification' the SCC code is linked to a name, 'Short.Name'. And so I want to loop through the 'data' frame's SCC codes and add the Short.Name value contained in the 'classification' to the 'data' frame based on the SCC identifier (I had to create data$Short.Name as a new variable to do this).

Note: I had to convert the values in both SCC columns to numeric as the SCC values in the 'data' frame are character-format and in the 'classfication' they are in the factor-format.

But, I get the error:

Error in if (as.numeric(data$SCC[i]) == as.numeric.factor(classification$SCC[j])) { : 
  missing value where TRUE/FALSE needed

I am guessing this is because the loop 'j' is longer than 'i' since length(classification$SCC) is larger than it is in the 'data' frame.

Is there a better way to couple the unique identifiers to their corresponding names, as described in another file?

Thanks in advance!

0

There are 0 best solutions below