如何根据 R 中的行值在 data.frame 中创建新列? [英] How to create new columns in a data.frame based on row values in R?

查看:28
本文介绍了如何根据 R 中的行值在 data.frame 中创建新列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿嘿

我有一个包含家庭三人组的 data.frame,我想添加一个列,其中包含每个id"(= 后代)的完整同胞.

I have a data.frame with family trios, and I would like to add a column with the full sibs of every "id" (= offspring).

我的数据:

df
         id    dam    sire
1:    83295  67606   79199
2:    83297  67606   79199
3:    89826  67606   79199

我想检索的内容:

df2
         id    dam    sire     fs1     fs2
1:    83295  67606   79199   83297   89826  
2:    83297  67606   79199   83295   89826  
3:    89826  67606   79199   83295   83297  

我尝试过的:

(类似于:如何转换数据帧在 R 中排成列?)

library(dplyr)
library(splitstackshape)

df2 <- df %>%
  group_by(dam,sire) %>%
  summarise(id = toString(id)) %>%
  cSplit("id") %>%
  setNames(paste0("fs_", 1:ncol(.)))

colnames(df2) <- c("dam", "sire", "id", "fs1", "fs2")

每个双亲只给我一行(而不是为每个id"创建相同的行):

Which only gives me one row per parent duo (instead of creating the same row per every "id"):

df2
     dam    sire       id      fs1     fs2
1: 67606   79199    83295    83297    89826  

在某些情况下不会有完整的同胞,而在某些情况下会有 15 个.

In some cases there will be no full sibs, and in some cases there will be 15.

提前感谢您的建议!:)

Thanks in advance for your advice! :)

推荐答案

我们可以group_by damsire 得到所有的id 除了当前的 id 使用 setdiff 然后使用 cSplit 将逗号分隔的值分隔到不同的列中.

We can group_by dam and sire get all id's except current id using setdiff and then use cSplit to separate comma-separated values into different columns.

library(splitstackshape)
library(dplyr)

df %>%
  group_by(dam, sire) %>%
  mutate(fs = purrr::map_chr(id, ~toString(setdiff(id, .x)))) %>%
  cSplit("fs")

#      id   dam  sire  fs_1  fs_2
#1: 83295 67606 79199 83297 89826
#2: 83297 67606 79199 83295 89826
#3: 89826 67606 79199 83295 83297

数据

df <- structure(list(id = c(83295L, 83297L, 89826L), dam = c(67606L, 
67606L, 67606L), sire = c(79199L, 79199L, 79199L)), class = "data.frame",
row.names = c("1:", "2:", "3:"))

这篇关于如何根据 R 中的行值在 data.frame 中创建新列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆