2015-03-31 40 views
0

的想法與其他DF的參考,例如提取DF本地字符的位置:[R據幀比較這,縮放壞

L<-LETTERS[1:25] 
A<-c(1:25) 
df<-data.frame(L,A) 
Compare<-c(LETTERS[sample(1:25, 25)]) 
df[] <- lapply(df, as.character) 
for (i in 1:nrow(df)){ 
    df[i,1]<-which(df[i,1]==Compare) 
} 
head(df) 

    L A 
1 14 1 
2 12 2 
3 2 3 

這工作不錯,但規模很不好,像所有,任何想法應用,還是dplyr?

感謝

+1

你的問題不清楚。你的預期輸出是什麼? – Thomas 2015-03-31 10:54:20

回答

2

只需使用match

你的數據(使用set.seed提供使用sample數據時)

df <- data.frame(L = LETTERS[1:25], A = 1:25) 
set.seed(1) 
Compare <- LETTERS[sample(1:25, 25)] 

解決方案

df$L <- match(df$L, Compare) 
head(df) 
# L A 
# 1 10 1 
# 2 23 2 
# 3 12 3 
# 4 11 4 
# 5 5 5 
# 6 21 6 
+1

謝謝大衛!這是這個!非常感謝! – user3615798 2015-04-01 09:27:05