如何基于R中的列对数据帧进行分类? [英] How to Classify data frame Based on a Columns in R?
本文介绍了如何基于R中的列对数据帧进行分类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框,并且具有这样的列:
I have a data frame and has columns like this:
gene col1 col2 type
------------------------------
gene_1 a b 1
gene_2 aa bb 2
gene_3 a b 1
gene_4 aa bb 2
我想使用列 col2查找列 type,然后 col1。所以我需要基于 col2和 col1的分类。
I want to find the column "type" using column "col2" and "col1". so I need a classification based on "col2" and "col1". how should I do this in R?
非常感谢
推荐答案
基于。在输出中,一个选项是从列'col1'和'col2'创建组索引
Based. on the output, an option is to create group indices from columns 'col1', and 'col2'
library(dplyr)
df1 %>%
mutate(type = group_indices(., col1, col2))
#. gene col1 col2 type
#1 gene_1 a b 1
#2 gene_2 aa bb 2
#3 gene_3 a b 1
#4 gene_4 aa bb 2
如果有多个名称,则一种选择是将字符串列名称转换为 sym
bols然后评估( !!!
)
If there are multiple names, then one option is to convert the string column names to sym
bols and then evaluate (!!!
)
df1 %>%
mutate(type = group_indices(., !!! rlang::syms(names(.)[2:3])))
或在 data.table
library(data.table)
setDT(df1)[, type := .GRP, .(col1, col2)]
数据
data
df1 <- structure(list(gene = c("gene_1", "gene_2", "gene_3", "gene_4"
), col1 = c("a", "aa", "a", "aa"), col2 = c("b", "bb", "b", "bb"
), type = c(1L, 2L, 1L, 2L)), class = "data.frame", row.names = c(NA,
-4L))
这篇关于如何基于R中的列对数据帧进行分类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文