R:如何用不同数据框中的另一列替换一列?

发布于 2025-01-16 00:13:30 字数 216 浏览 1 评论 0原文

我想将 ped 数据帧的 第 6 列 替换为 bipolar_ctl 数据帧中的 Phenotype 列。

我的尝试:

dplyr::mutate_at(ped, vars(-one_of("bipolar_ctl$Phenotype")))

I want to replace the 6th column of the ped dataframe with the Phenotype column from bipolar_ctl dataframe.

My attempt:

dplyr::mutate_at(ped, vars(-one_of("bipolar_ctl$Phenotype")))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

永言不败 2025-01-23 00:13:30

使用基础R

ped[,6] = bipolar_ctl$Phenotype

Using base R,

ped[,6] = bipolar_ctl$Phenotype
一枫情书 2025-01-23 00:13:30

另一种选择是:

ped$sixth_col = bipolar_ctl$Phenotype

another option would be:

ped$sixth_col = bipolar_ctl$Phenotype
尹雨沫 2025-01-23 00:13:30

注意两个数据集中可能出现不同的行顺序,这在较长的脚本中很容易发生。

您可以使用 match 轻松考虑这一点:

ped[,6] <- bipolar_ctl[match(ped$id1, bipolar_ctl$id2), ]$Phenotype
ped
#   id1        V2        V3        V4        V5        V6        V7
# 1   1 0.9148060 0.5190959 0.4577418 0.9400145 0.4357716 0.5142118
# 2   2 0.9370754 0.7365883 0.7191123 0.9782264 0.9066014 0.3902035
# 3   3 0.2861395 0.1346666 0.9346722 0.1174874 0.6117786 0.9057381
# 4   4 0.8304476 0.6569923 0.2554288 0.4749971 0.2076590 0.4469696
# 5   5 0.6417455 0.7050648 0.4622928 0.5603327 0.3795592 0.8360043

或者使用 merge

merge(ped[-6], bipolar_ctl[c('id2', 'Phenotype')], by.x='id1', by.y='id2')
#   id        V2        V3        V4        V5        V7 Phenotype
# 1  1 0.9148060 0.5190959 0.4577418 0.9400145 0.5142118 0.4357716
# 2  2 0.9370754 0.7365883 0.7191123 0.9782264 0.3902035 0.9066014
# 3  3 0.2861395 0.1346666 0.9346722 0.1174874 0.9057381 0.6117786
# 4  4 0.8304476 0.6569923 0.2554288 0.4749971 0.4469696 0.2076590
# 5  5 0.6417455 0.7050648 0.4622928 0.5603327 0.8360043 0.3795592

或者使用 stopifnot

stopifnot(identical(ped$id1, bipolar_ctl$id2))
# Error: identical(ped$id1, bipolar_ctl$id2) is not TRUE

数据(具有不同的 ID 顺序):

set.seed(42)
ped <- cbind(id1=1:5, matrix(runif(30), 5, 6)) |> as.data.frame()
bipolar_ctl <- data.frame(id2=sample(1:5), Phenotype=runif(5))

Beware of the possible occurrence of different row orders in the two data sets, which easily may happen in a long script.

You can easily take this into account by using match:

ped[,6] <- bipolar_ctl[match(ped$id1, bipolar_ctl$id2), ]$Phenotype
ped
#   id1        V2        V3        V4        V5        V6        V7
# 1   1 0.9148060 0.5190959 0.4577418 0.9400145 0.4357716 0.5142118
# 2   2 0.9370754 0.7365883 0.7191123 0.9782264 0.9066014 0.3902035
# 3   3 0.2861395 0.1346666 0.9346722 0.1174874 0.6117786 0.9057381
# 4   4 0.8304476 0.6569923 0.2554288 0.4749971 0.2076590 0.4469696
# 5   5 0.6417455 0.7050648 0.4622928 0.5603327 0.3795592 0.8360043

Or, use merge.

merge(ped[-6], bipolar_ctl[c('id2', 'Phenotype')], by.x='id1', by.y='id2')
#   id        V2        V3        V4        V5        V7 Phenotype
# 1  1 0.9148060 0.5190959 0.4577418 0.9400145 0.5142118 0.4357716
# 2  2 0.9370754 0.7365883 0.7191123 0.9782264 0.3902035 0.9066014
# 3  3 0.2861395 0.1346666 0.9346722 0.1174874 0.9057381 0.6117786
# 4  4 0.8304476 0.6569923 0.2554288 0.4749971 0.4469696 0.2076590
# 5  5 0.6417455 0.7050648 0.4622928 0.5603327 0.8360043 0.3795592

Alternatively use a stopifnot.

stopifnot(identical(ped$id1, bipolar_ctl$id2))
# Error: identical(ped$id1, bipolar_ctl$id2) is not TRUE

Data (with different id order):

set.seed(42)
ped <- cbind(id1=1:5, matrix(runif(30), 5, 6)) |> as.data.frame()
bipolar_ctl <- data.frame(id2=sample(1:5), Phenotype=runif(5))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文