Problem: I have a dataframe (df)
Source Source1 Target Target.1 Target.2 Target.3
1 SDB1 SDB2 TDB1 TDB2 TDB2 TDB2
2 STB1 STB2 TTB1 TTB2 TTB2 TTB3
3 SCOL1 SCOL2 TCOL1 TCOL2 TCOL3 TCOL4
I want a resultant dataframe which would look like (What I want is to get Unique pairs from rows 1 and 2):
X1 X2
SDB1 STB1
SDB2 STB2
TDB1 TTB1
TDB2 TTB2,TTB3
What I tried
I was able to get this till now and not sure how to proceed from here:
!(duplicated(t(df[c(1,2),])))
[1] TRUE TRUE TRUE FALSE TRUE
We can subset first two rows of dataframe and do aggregate
df1 <- as.data.frame(t(df[1:2, ]), row.names = FALSE)
names(df1) <- paste0('X', 1:2)
aggregate(X2~X1, df1, function(x) toString(unique(x)))
# X1 X2
#1 SDB1 STB1
#2 SDB2 STB2
#3 TDB1 TTB1
#4 TDB2 TTB2, TTB3
Using dplyr
, we can do :
library(dplyr)
df %>%
slice(1:2) %>%
t %>%
as.data.frame() %>%
group_by(V1) %>%
summarise(V2 = toString(unique(V2)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.