i've been struggling while , can't find way out. here's problem.
i have 2 dataframes:
df1 <- data.frame(replicate(3,sample(1:10,20,rep=true))) df1 x1 x2 x3 1 10 1 9 2 3 4 2 3 7 6 8 4 8 10 7 5 5 7 5 6 8 5 9 7 9 8 4 8 6 2 7 9 2 9 6 10 5 2 9 df2 <- data.frame(df1[sample(nrow(df1),4), ]) df2 x1 x2 x3 8 6 2 7 3 7 6 8 10 5 2 9 7 9 8 4 i create vector x of length(x) = length(df1) containing, per each row of df1, row index of corresponding row in df2 (i.e. same exact values each column between df1 , df2).
consider that:
dim(df1) [1] 1096188 3 dim(df2) [1] 256 3 and df1 has several rows same values (i.e. corresponding row index same), , in principle rows in df1 should find match row in df2.
the expected output be:
x [1] 0 0 2 0 0 0 4 1 0 3 hope clear enough...
can help?
thanks,
piera
i'd try:
x <- rownames(df2)[match(do.call(paste, df1), do.call(paste, df2))] x[is.na(x)] <- 0 there quite discussion on it's desired output; in @cathg interpretation, line produces it:
match(do.call(paste, df1), do.call(paste, df2),nomatch=0)
Comments
Post a Comment