列提取唯一行我有一个数据帧:[R数据框 - 从
source= c("A", "A", "B")
target = c("B", "C", "C")
source_A = c(5, 5, 6)
target_A = c(6, 7, 7)
source_B = c(10, 10, 11)
target_B = c(11, 12, 12)
c = c(0.5, 0.6, 0.7)
df = data.frame(source, target, source_A, target_A, source_B, target_B, c)
> df
source target source_A target_A source_B target_B c
1 A B 5 6 10 11 0.5
2 A C 5 7 10 12 0.6
3 B C 6 7 11 12 0.7
如何减少这个数据框为独特的源和目标值仅返回值和返回(忽略列C)。
的价值观[A B C]
id A B
1 A 5 10
2 B 6 11
3 C 7 12
目前,我做这样的事:
df1 <- df[,c("source","source_A", "source_B")]
df2 <- df[,c("target","target_A", "target_B")]
names(df1)[names(df1) == 'source'] <- 'id'
names(df1)[names(df1) == 'source_A'] <- 'A'
names(df1)[names(df1) == 'source_B'] <- 'B'
names(df2)[names(df2) == 'target'] <- 'id'
names(df2)[names(df2) == 'target_A'] <- 'A'
names(df2)[names(df2) == 'target_B'] <- 'B'
df3 <- rbind(df1,df2)
df3[!duplicated(df3$id),]
id A B
1 A 5 10
3 B 6 11
5 C 7 12
在现实中,我有几列的,所以这是不可行的长远。
我怎样才能更简洁地完成这项工作(理想情况下,对于更多的专栏可以概括)?
的'source'和'target'值相同'id'总是相同? – LAP
@LAP是的(或者我已经搞砸了......)其他列的A值总是特定于A(即使它们对于每列都不相同)。 – Chuck