根据R中另一个数据帧的规则,向一个数据帧添加多个变量
这是我的数据的一个示例(都是data.frames): 表1:根据R中另一个数据帧的规则,向一个数据帧添加多个变量,r,dataframe,R,Dataframe,这是我的数据的一个示例(都是data.frames): 表1: 0_x 1_x 2_x .... 20_x cat cat red......green dog red green cat bee blue bee.... dog ........ 和表2 x name code cat animals 1 dog animals 1 bee animals. 1 green colours 2 red c
0_x 1_x 2_x .... 20_x
cat cat red......green
dog red green cat
bee blue bee.... dog
........
和表2
x name code
cat animals 1
dog animals 1
bee animals. 1
green colours 2
red colours. 2
...
我希望得到的结果如下:
0_y 1_y 2_y .... 20_y 0_x 1_x 2_x .... 20_x
1 1 2..... 2 cat cat red......green
1 2 2 1 dog red green cat
1 2 1.... 1 ....
........
基本上,表2包含一条规则,我想用它来创建要添加到表1中的变量
如果0_x是一个cat,我希望0_y等于1(因为在表2中cat=1)
如何以优雅的方式获得此结果?(如果我只有一个变量0_x,我只会进行一次合并,但这里我有几个)您可以在表2
x
列和您正在读取的列之间匹配您的值。下面是我在for循环中使用它的示例
注:考虑到DF1的姓氏必须先有字母,而不是数字,并且我使用字符串。
df1 <- data.frame(x_0 = c('cat','dog','bee'),
x_1 = c('cat','red','blue') ,
x_2 = c('red','green','bee') )
df2 <- data.frame(x = c('cat','dog','bee','green','red','blue'),
name = c('animals','animals','animals','colours','colours','colours'),
code = c(1,1,1,2,2,2))
df1b = df1 ; colnames(df1b) <- sub("x","y",colnames(df1b))
df3 = cbind(df1b,df1)
for(i in 1:ncol(df1)){
df3[,i] <- df2$code[match(df1[,i],df2$x)]
}
df3
# y_0 y_1 y_2 x_0 x_1 x_2
# 1 1 1 2 cat cat red
# 2 1 2 2 dog red green
# 3 1 2 1 bee blue bee
df1您可以在表2x
列和您正在读取的列之间匹配您的值。下面是我在for循环中使用它的示例
注:考虑到DF1的姓氏必须先有字母,而不是数字,并且我使用字符串。
df1 <- data.frame(x_0 = c('cat','dog','bee'),
x_1 = c('cat','red','blue') ,
x_2 = c('red','green','bee') )
df2 <- data.frame(x = c('cat','dog','bee','green','red','blue'),
name = c('animals','animals','animals','colours','colours','colours'),
code = c(1,1,1,2,2,2))
df1b = df1 ; colnames(df1b) <- sub("x","y",colnames(df1b))
df3 = cbind(df1b,df1)
for(i in 1:ncol(df1)){
df3[,i] <- df2$code[match(df1[,i],df2$x)]
}
df3
# y_0 y_1 y_2 x_0 x_1 x_2
# 1 1 1 2 cat cat red
# 2 1 2 2 dog red green
# 3 1 2 1 bee blue bee
df1也许您可以使用以下基本R解决方案,使用match()
+unlist()
数据
df1 <- structure(list(`0_x` = c("cat", "dog", "bee"), `1_x` = c("cat",
"red", "blue"), `2_x` = c("red", "green", "bee"), `20_x` = c("green",
"cat", "dog")), class = "data.frame", row.names = c(NA, -3L))
df2 <- structure(list(x = c("cat", "dog", "bee", "green", "red"), name = c("animals",
"animals", "animals.", "colours", "colours."), code = c(1L, 1L,
1L, 2L, 2L)), class = "data.frame", row.names = c(NA, -5L))
df1也许您可以使用以下基本R解决方案,使用match()
+unlist()
数据
df1 <- structure(list(`0_x` = c("cat", "dog", "bee"), `1_x` = c("cat",
"red", "blue"), `2_x` = c("red", "green", "bee"), `20_x` = c("green",
"cat", "dog")), class = "data.frame", row.names = c(NA, -3L))
df2 <- structure(list(x = c("cat", "dog", "bee", "green", "red"), name = c("animals",
"animals", "animals.", "colours", "colours."), code = c(1L, 1L,
1L, 2L, 2L)), class = "data.frame", row.names = c(NA, -5L))
df1好奇的是,你为什么想要这种宽格式的数据结构?大多数数据科学需要(合并、附加、聚合、建模、绘图)最好是以长而整齐的格式完成。@Parfait我基本上想重新标记一系列分类变量(共享相同的结构)根据我在另一个文件中的父子关系(猫、狗等都是动物等),我将模型分为较少的类别与目前的观测数量相比,我有太多的类别,我想找到一种有意义地使用这些信息的方法。希望它是清楚的。好奇的是,你为什么想要这种格式广泛的数据结构?大多数数据科学需要(合并、追加、聚合、建模、绘图)最好以长而整洁的格式完成。@Parfait我基本上想根据另一个文件中的父子关系(猫、狗等是动物等)将一系列用于模型的分类变量(共享相同的结构)重新标记为较少的类别与我目前的观察数量相比,我有太多的类别,我想找到一种有意义地使用这些信息的方法。希望这是清楚的。需要更改为df3=cbind(df1b,df1),它工作得很好的一部分经过编辑。我打算放的第一个代码没有包括对colnames的修改,因此df3=cbind(df1,df1)
变得更加合理。需要更改为df3=cbind(df1b,df1),它工作得很好的一部分经过编辑。我打算放的第一个代码没有包括对colnames的修改,因此df3=cbind(df1,df1)
更加合理。