0

I have data dat like this:

s    A     chan
10   0.1   1
20   0.2   1
30   0.3   1
40   0.5   1
50   0.7   1
60   0.5   1
10   0.1   2
20   0.3   2
30   0.4   2
40   0.5   2
50   0.6   2
60   0.6   2
10   0.2   3
20   0.2   3
30   0.3   3
40   0.4   3
50   0.5   3
40   0.7   3
10   0.2   4
20   0.2   4
30   0.3   4
40   0.3   4
50   0.6   4
60   0.8   4

and I want to subset my data frame dat based on s (time) for each chan (channel) with a data frame df like this

s    chan
10    1
20    2
30    3
40    4

If I use dat %>% filter(s %in% df$s) I get each value for every channel like this:

s    A     chan
10   0.1   1
20   0.2   1
30   0.3   1
40   0.5   1
10   0.1   2
20   0.3   2
30   0.4   2
40   0.5   2
10   0.2   3
20   0.2   3
30   0.3   3
40   0.4   3
10   0.2   4
20   0.2   4
30   0.3   4
40   0.3   4

but what I actualy want it this:

s    A     chan
10   0.1   1
20   0.3   2
30   0.3   3
40   0.3   4

How can I achieve this result?

2
  • dat %>% filter(s == chan) Commented Jan 27, 2022 at 12:27
  • ok, in this case it would acutally work, I should have used differnt numbers for s. Commented Jan 27, 2022 at 12:38

2 Answers 2

2

what you are looking for is semi_join; it filters rows from left data frame based on the presence or absence of matches in right data frame,

semi_join(dat, df, by = c("s", "chan"))
Sign up to request clarification or add additional context in comments.

1 Comment

That does the trick with my test data, thanks a lot!
0

I think this should do it

dat[which(dat[,3]==df[1:4,2] & dat[,1]==df[1:4,1]),]

1:4 being the range of lines in df.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.