根据另一列的值复制一列的值
Copying over the values of a column, based on the values of another column
我有一个数据集,"data",其中每隔几个相邻的行都有相同的 "id",数据集中的一个参数。我有另一个数据集 "otherData",具有相同的 ID 值,但每个 ID 值只有一行。两个数据集中的 id 顺序相同。
我想从 otherData 复制一列 "newColumn" 到 data,根据 id 值映射它。这是我目前拥有的:
sapply(otherData$id, function(id)
data$newColumn[data$id == id, ] <- otherData[otherData$id == id, ]$newColumn
)
但是,sapply 慢得令人作呕,因为它遍历每个唯一的 id 值,并且必须在 data.frame.
中找到具有该值的每一行
有没有更快的选择?我想也许我可以利用这样一个事实,即具有相同 "id" 值的所有行都彼此相邻。
只需合并两个数据框,但过滤 otherData
中的列以加入 id 键和 newColumn:
newData <- merge(data, otherData[c("id", "newColumn")], by=c("id"))
我有一个数据集,"data",其中每隔几个相邻的行都有相同的 "id",数据集中的一个参数。我有另一个数据集 "otherData",具有相同的 ID 值,但每个 ID 值只有一行。两个数据集中的 id 顺序相同。
我想从 otherData 复制一列 "newColumn" 到 data,根据 id 值映射它。这是我目前拥有的:
sapply(otherData$id, function(id)
data$newColumn[data$id == id, ] <- otherData[otherData$id == id, ]$newColumn
)
但是,sapply 慢得令人作呕,因为它遍历每个唯一的 id 值,并且必须在 data.frame.
中找到具有该值的每一行有没有更快的选择?我想也许我可以利用这样一个事实,即具有相同 "id" 值的所有行都彼此相邻。
只需合并两个数据框,但过滤 otherData
中的列以加入 id 键和 newColumn:
newData <- merge(data, otherData[c("id", "newColumn")], by=c("id"))