比较三个分类列并用 0,1 或 2 表示结果

comparing three categorical columns and representing the results with 0,1 or 2

如何将我的一个专栏的分类内容与其他 3 个专栏的内容进行比较?比如x的数据和y的数据一样就用0表示,和v一样就用1表示,和z一样就用2表示。

x<- c(C,T,T,G,A,A,T,C,G)
y<- c(C,G,G,T,G,C,C,A,T)
v<- c(A,C,T,C,G,G,T,A,C)
z<- c(CTC,CCT,ATC,AC,T,CC,GC,AC,CTAC)

你好!

您可以使用 dyplr 及其 case_when() 参数来定义多个 if-statements:

# define data
x<- c("C","T","T","G","A","A","T","C","G")
y<- c("C","G","G","T","G","C","C","A","T")
v<- c("A","C","T","C","G","G","T","A","C")
z<- c("CTC","CCT","ATC","AC","T","CC","GC","AC","CTAC")

dat <- data.frame(as.character(x), as.character(y), as.character(v), as.character(z))

# create new column considering your desired conditions
dat <- dat %>%
  mutate(new_column = case_when(x == y ~ 0,
                                x == v ~ 1,
                                x == z ~ 2,
                                TRUE ~ NA_real_))