Skip to content

Instantly share code, notes, and snippets.

@anarosner
Created August 17, 2016 18:20
Show Gist options
  • Save anarosner/c3d04f44f9dfea1dc8ff3c4ae15014f8 to your computer and use it in GitHub Desktop.
Save anarosner/c3d04f44f9dfea1dc8ff3c4ae15014f8 to your computer and use it in GitHub Desktop.
create_relational <- function( df, col, id_col="join_id", join_name="join_name" ) {
if( !is.factor(df[,col]) )
df[,col] <- as.factor(df[,col])
df[,id_col] <- unclass( df[,col] )
join_table <- data.frame( x=attr(unclass(df[,col]),"levels") )
names(join_table) <- join_name
join_table[,id_col] <- 1:length(attr(unclass(df[,col]),"levels"))
join_table[,join_name] <- as.character( join_table[,join_name] )
return( list(df=df, join_table=join_table) )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment