[英]How to extract column names for each row using column values inR?
I have a dataframe that calculates the user similarity.我有一个计算用户相似度的 dataframe。
I want to extract a list of users with a similarity of 0.5 or higher for each user.我想为每个用户提取相似度为 0.5 或更高的用户列表。
like this...像这样...
Is there any way to get this result at once without using a loop?有没有办法在不使用循环的情况下立即获得这个结果?
Set the diagonal to 0 since you don't want to include the similarity of the user with itself and use apply
rowwise.将对角线设置为 0,因为您不想包含用户与其自身的相似性并使用逐行
apply
。
cols <- colnames(df)
diag(df) <- 0
result <- data.frame(userid = cols,
similar_user = apply(df >= 0.5, 1, function(x) toString(cols[x])))
result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.