Re: [R] subset rows in two dataframes

2008-05-11 Thread Zhuanshi He
Dear Jim, The following codes maybe helps. for (i in 1:length(dat1[,1])) { for (j in 1:length(dat2[,1])) { if (dat1[i,1] == dat2[j,1] & dat1[i,2] == dat2[j,2]) print (j) } } time1<- as.Date(c("2006-01-03", "2006-05-03", "2006-05-04", "2006-05-11", "2006-05-12", "2006-05-16", "2006

Re: [R] subset rows in two dataframes

2008-05-11 Thread Zhuanshi He
Dear Jim, Maybe u want this, > subset(dat2, time1 %in% dat2$v1 & time2 %in% dat2$v1) v1 v2 2 2006-05-09 7065.0 3 2006-05-04 3622.5 5 2006-07-14 3532.5 7 2006-05-12 6480.0 8 2006-05-17 4612.5 15 2006-07-05 4837.5 16 2006-07-06 3352.5 18 2006-07-24 6772.5 20 2006-07-18 5625.0 Wa

Re: [R] subset rows in two dataframes

2008-05-11 Thread jim holtman
Here is one way to compare the entire rows between the data frames: > x1 <- do.call(paste, dat1) > x2 <- do.call(paste, dat2) > dat1[x1 %in% x2,] [1] v1 v2 <0 rows> (or 0-length row.names) > > x1 [1] "2006-01-03 7312.5" "2006-05-03 3352.5" "2006-05-04 4252.5" "2006-05-11 3825" [5] "2006-05-12 27

Re: [R] subset rows in two dataframes

2008-05-11 Thread jim holtman
It is giving you exactly what you are asking for. You asked first which of dat1$v1 were in dat2$v; you got a TRUE on the second value (2006-05-03): > dat1$v1 %in% dat2$v1 [1] FALSE TRUE TRUE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE You then asked for which of dat1$v2 were in

Re: [R] subset rows in two dataframes

2008-05-11 Thread partofy
Not exactly. I need something to subset ONLY rows common to both dataframes. In the provided example, dat1 and dat2 have no common rows so I would expect: [1] v1 v2 <0 rows> (or 0-length row.names) But I canĀ“t do it... On Sun, 11 May 2008 10:07:25 -0400, "Zhuanshi He" <[EMAIL PROTECTED]> said:

Re: [R] subset rows in two dataframes

2008-05-11 Thread partofy
Dear list: I can now reproduce with a bit of my real data, the problem I asked for your help yestarday: time1<- as.Date(c("2006-01-03", "2006-05-03", "2006-05-04", "2006-05-11", "2006-05-12", "2006-05-16", "2006-05-19", "2006-05-26", "2006-09-15", "2006-10-30", "2006-11-08", "2006-11-14", "2006-1

Re: [R] subset rows in two dataframes

2008-05-10 Thread partofy
Thanks for your reply. In fact I dont get none error message, I just simply found that some rows do not match in both dataframes. I thought it was some evident problem with my code, but it seems it is not. I'll try to check and if I can, I'll post a reproducible example. Justin On Sat, 10 May

Re: [R] subset rows in two dataframes

2008-05-10 Thread jim holtman
This seems to work for me: > set.seed(1) > df1 <- data.frame(v1=factor(sample(1:4,20,TRUE)), > v2=factor(sample(1:3,20,TRUE)), v3=sample(1:3,20,TRUE)) > df2 <- data.frame(v1=factor(sample(1:2,20,TRUE)), > v2=factor(sample(1:2,20,TRUE)), v3=sample(1:2,20,TRUE)) > subset(df1, (df1$v1 %in% df2$v1)

[R] subset rows in two dataframes

2008-05-10 Thread partofy
Dear list: I have two dataframes, say dat1 and dat2. Each has several variables but 3 of each are common in both, (say v1, v2 and v3). v1 and v2 are factores while v3 is numeric. Now, I need a subset to extract the rows in which v1, v2 and v3 are the same in both dataframes. I tried: subset(dat1,