r - Position or row in which a term is found in vector -
i indicate possition or row of elements in vectors
df <- data.frame(g1=c("a","b","e","d","c"), g2=c("a","c","b","d","e")) in case g1 reference, "a" first, "b", second, "e", third , on. see in g2 "a" first, "b" third. can output:
df2 <- data.frame(g1=c(1:5), g2=c(1, 3, 5, 4, 2)) row.names(df2) <- c("a","b","e","d","c")
try match:
df2 <- data.frame(lapply(df, function(x) match(df$g1, x)), row.names=df$g1) # g1 g2 # 1 1 # b 2 3 # e 3 5 # d 4 4 # c 5 2
Comments
Post a Comment