Hey all I have a data frame df1 and I want to filter this data frame with another data frame df2.
I want to filter the columns of df1 with the rows of df2 so that I get a only the columns of df1 that are rows df2.
I tried the following code but an error occurred.
df1 %>% filter(colnames(df1) %in% rownames(df2))
The error
Error in `filter()`:
i In argument: `colnames(df1) %in% rownames(df2)`.
Caused by error:
! `..1` must be of size 46 or 1, not size 163.
Run `rlang::last_trace()` to see where the error occurred.
df1 is 46 * 163 and df2 151 * 6 big.
What am I doing wrong?
edit here is some dummy data
df1
sp1 sp2 sp3 sp4
1 0 1 1 2
2 1 1 1 0
3 0 2 0 1
4 0 1 1 0
5 1 1 0 1
df2
x1 x2 x3 x4
sp1 10 0.1 1 a
sp2 11 0.5 2 b
sp3 12 0.1 3 c
and my output should be:
sp1 sp2 sp3
1 0 1 1
2 1 1 1
3 0 2 0
4 0 1 1
5 1 1 0