在R中的数据框中显示相应的值 [英] Displaying corresponding values in data frame in R

查看:80
本文介绍了在R中的数据框中显示相应的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请检查下面的代码,我使用下面的三个变量创建了一个数据框,变量 y123计算出a2列与a1之间的相似度。变量 y123为我提供了总共16个值,其中每个a1值都与a2进行比较。我的需要是,当将特定的 a1值与特定的 a2值进行比较时,我希望在 a2旁边显示相应的 a3值。因此,结果应该是一个数据帧,该数据帧的列为y123,而第二列的对应 a3列出现了四次,即16个值。谢谢,请帮助。

Please check the code below, I have created a data frame using three variables below, the variable "y123" computes the similarity between columns a2 with a1. The variable "y123" gives me total 16 values where every a1 value gets compared with a2. My need is that when a particular "a1" value is compared with a particular "a2" value, I want the corresponding "a3" value next to "a2" be displayed besides. So the result should be a data frame with column y123 and a second column with corresponding "a3" column appearing four times i.e 16 values. Thanks and please help.

library(stringdist)
library(RecordLinkage)
a1 = c(103,120,142,153)
a2 = c(113,453,142,102)
a3 = c("a1","b1","c1","d1")
a1 = as.character(a1)
a2 = as.character(a2)
a3 = as.character(a3)
a123 = data.frame(a1,a2,a3)
y123 = sapply(a1, function(i) RecordLinkage::levenshteinSim(i,a2))
b1 = c(y123)
b1

我需要列出以下内容:

new_data = data.frame(b1,new_column)


推荐答案

我认为这就是您想要的。我修改了您的 sapply 函数:

I think this is what you want. I modified your sapply function:

data.frame(y123 = c(y123), a3 = rep(a3, times = length(a3)))
#        y123 a3
#1  0.6666667 a1
#2  0.3333333 b1
#3  0.3333333 c1
#4  0.6666667 d1
#5  0.3333333 a1
#6  0.0000000 b1
#7  0.3333333 c1
#8  0.3333333 d1
#9  0.3333333 a1
#10 0.0000000 b1
#11 1.0000000 c1
#12 0.6666667 d1
#13 0.6666667 a1
#14 0.6666667 b1
#15 0.3333333 c1
#16 0.3333333 d1

这篇关于在R中的数据框中显示相应的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆