Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何连接具有不同数据类型的两个数据?_R_Left Join - Fatal编程技术网

R 如何连接具有不同数据类型的两个数据?

R 如何连接具有不同数据类型的两个数据?,r,left-join,R,Left Join,假设我有两个数据。 一个是这样的: names |height ------|------ "ab" |176 ------|-------- "aa" |168 ------|-------- "ac" |189 ------|-------- "ad" |179 names weight c("aa","ab") 58 c("ac","ae") 70 "ad" 68 names height weight "ab" 1

假设我有两个数据。 一个是这样的:

names |height
------|------
"ab"  |176
------|--------
"aa"  |168
------|--------
"ac"  |189
------|--------
"ad"  |179
names           weight
c("aa","ab")    58
c("ac","ae")    70
"ad"            68
names  height  weight
"ab"   176     58
"aa"   168     58
"ac"   189     70
"ad"   179     68
另一个是这样的:

names |height
------|------
"ab"  |176
------|--------
"aa"  |168
------|--------
"ac"  |189
------|--------
"ad"  |179
names           weight
c("aa","ab")    58
c("ac","ae")    70
"ad"            68
names  height  weight
"ab"   176     58
"aa"   168     58
"ac"   189     70
"ad"   179     68
第二个名字是列表,但第一个名字只是向量。 我想这样做:

names |height
------|------
"ab"  |176
------|--------
"aa"  |168
------|--------
"ac"  |189
------|--------
"ad"  |179
names           weight
c("aa","ab")    58
c("ac","ae")    70
"ad"            68
names  height  weight
"ab"   176     58
"aa"   168     58
"ac"   189     70
"ad"   179     68
我试着用left_-join,但没用。 我也试着把列表变成向量。 当我将列表转换为向量时,问题是长度彼此不同。 求求你,你能帮我吗??? 这是我关于stackoverflow的第一个问题

添加我的代码

names<-c("ab","aa","ac","ad")
height<-c(176,168,189,179)
data1<-cbind(names,height)
names<-list(c("aa","ab"),c("ac","ae"),"ad")
weight<-c(58,70,68)
data2<-cbind(names,weight)
data1<-as.data.frame(data1) ;data1;str(data1)
data2<-as.data.frame(data2) ;data2;str(data2)
data2 %>%

names我们可以使用
tidyverse
假设“names”列是第二个数据集中的
列表。使用
unnest
,将其转换为
向量
,然后通过
的“名称”将
与第一个数据集
左连接

library(tidyverse)
df2 %>%
   unnest %>%
   left_join(df1, ., by = "names") 
#   names height weight
#1    ab    176     58
#2    aa    168     58
#3    ac    189     70
#4    ad    179     68
数据
df1欢迎来到StackOverflow!请阅读相关信息以及如何给出建议。这将使其他人更容易帮助你。请共享你的代码,即使它不起作用。好的,我将用一些代码编辑更多。哦,它只是自动改变了自己!!!!您正在使用
cbind
创建一个矩阵,这将给您带来更多问题,因为矩阵只能接受一个类。因此,它将数字转换为字符,因为存在字符元素,然后需要进行重新转换等。。相反,使用
data1您能解释更多吗??什么是unnest???@DosanHwang我添加了一些解释
unnest
是一个来自
tidyr
的函数,它是加载
library(tidyverse)
的软件包之一,很抱歉,我得到了一个错误。UseMethod(“unnest_uu2;”)中的错误:“unnest_2;”没有适用于“c”(“矩阵”、“列表”)类对象的方法我该怎么办?@DosanHwang看起来你有一个
矩阵
用于
df2
将其转换为
data.frame
I..e
df2%>%as.data.frame%>%unest
并检查它是否有效现在我得到了这个错误。错误:所有嵌套列的元素数必须相同。