简体   繁体   English

根据其他列中的值将数据添加到列中

[英]Add data to column based on values from other columns

I have a dataset, which looks like this 我有一个数据集,看起来像这样

Col1  Col2  Col3
A       B    NA
AA      C    NA
D       CC   NA
E       F    NA

I would like to add data to col3 based on condition. 我想根据条件将数据添加到col3。 If col1 and col2 only have one letter each, write "SNP". 如果col1和col2各自只有一个字母,请输入“ SNP”。 If col1 has more than one letter, write "DEL" and if col2 has more than one letter, write "INS" 如果col1有多个字母,请写“ DEL”,如果col2有多个字母,请写“ INS”

Final product would be: 最终产品将是:

Col1  Col2  Col3
A       B    SNP
AA      C    DEL
D       CC   INS
E       F    SNP

Anyone would know how to do this in R? 任何人都知道如何在R中执行此操作吗?

Thank you! 谢谢!

You can use two nested ifelse statements. 您可以使用两个嵌套的ifelse语句。 So, for example, using dplyr::mutate : 因此,例如,使用dplyr::mutate

library(dplyr)
df = df %>% mutate(Col3 = ifelse(nchar(Col1)>1,"DEL",ifelse(nchar(Col2)>1,"INS","SNP")))

  Col1 Col2 Col3
1    A    B  SNP
2   AA    C  DEL
3    D   CC  INS
4    E    F  SNP

A tidyverse solution: tidyverse解决方案:

library(magrittr); library(dplyr); library(stringr)

df %>% mutate(Col3 = case_when(str_length(Col1) == 1 & str_length(Col2) == 1 ~ "SNP",
                                 str_length(Col1) > 1 ~ "DEL",
                                 str_length(Col2) > 1 ~ "INS")
                )

  Col1 Col2 Col3
1    A    B  SNP
2   AA    C  DEL
3    D   CC  INS
4    E    F  SNP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM