我有两个数据框:
数据帧1:
ColA ColB
Mat Student
Por Yes
数据框2:
ColA ColB
Mat 0
Student 1
Por 2
Yes 3
No 4
我想通过用数据帧2替换数据帧1中的值来创建一个新的数据帧
预期输出:
ColA ColB
0 1
2 3
答案 0 :(得分:4)
使用一些重塑和连接的替代解决方案:
dt1 = read.table(text = "
ColA ColB
Mat Student
Por Yes
", header=T, stringsAsFactors=F)
dt2 = read.table(text = "
ColA ColB
Mat 0
Student 1
Por 2
Yes 3
No 4
", header=T, stringsAsFactors=F)
library(tidyverse)
dt1 %>%
gather(x,y) %>% # reshape data
left_join(dt2, by=c("y"="ColA")) %>% # join values from 2nd table
group_by(x) %>% # for each value of this column
mutate(y = row_number()) %>% # count rows (useful to reshape again)
spread(x,ColB) %>% # reshape again
select(-y) # remove column
# # A tibble: 2 x 2
# ColA ColB
# <int> <int>
# 1 0 1
# 2 2 3
以及另一种使用用户创建的功能的解决方案,该解决方案适用于所有列:
# function that gets ColB from 2nd dataset based on what ColA is
# Vectorised version
f = function(x) dt2$ColB[dt2$ColA==x]
f = Vectorize(f)
# apply function to all columns
dt1 %>% mutate_all(f)
# ColA ColB
# 1 0 1
# 2 2 3
答案 1 :(得分:3)
遍历列和子集:
# example data
df1 <- read.table(text = "
ColA ColB
Mat Student
Por Yes
", header = TRUE, stringsAsFactors = FALSE)
df2 <- read.table(text = "
ColA ColB
Mat 0
Student 1
Por 2
Yes 3
No 4
", header = TRUE, stringsAsFactors = FALSE)
# matrix output
sapply(df1, function(i) df2[ df2$ColA %in% i, "ColB"])
# ColA ColB
# [1,] 0 1
# [2,] 2 3
# data.frame output
data.frame(lapply(df1, function(i) df2[ df2$ColA %in% i, "ColB"]))
# ColA ColB
# 1 0 1
# 2 2 3
答案 2 :(得分:3)
这是一个使用dplyr库并在两个数据帧之间执行联接的笨拙解决方案。
df1<-read.table(header=TRUE, text="ColA ColB
Mat Student
Por Yes", stringsAsFactor=FALSE)
df2<-read.table(header=TRUE, text="ColA ColB
Mat 0
Student 1
Por 2
Yes 3
No 4", stringsAsFactor=FALSE)
library(dplyr)
newColA<-left_join(df1, df2, by="ColA")
newColB<-left_join(df1, df2, by=c("ColB" = "ColA"))
answer<-data.frame(ColA= newColA[, "ColB.y"], ColB= newColB[, "ColB.y"])
# ColA ColB
# 1 0 1
# 2 2 3