选择(多个)整数,每行出现n次 [英] Select (multiple) integers with n occurrences per row

查看:219
本文介绍了选择(多个)整数,每行出现n次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个data.frame,其中数据条目以 1,2,3,10 格式输入。也就是说,它们是逗号分隔的整数,范围从0-20,并且不需要是连续的。每个目前被认为是一个因素。我有四个变量包含这些值,我想创建一个新的变量,包括一个给定的整数,只有当它在四个变量中的三个,如果没有出现三个整数,然后使用0 。

I have a data.frame where the data entries are entered in this format 1,2,3,10. That is, they are comma separated integers that range from 0-20, and do not need to be consecutive. Each is currently considered a factor. I have four variables that contain these values, and I'd like to create a new variable, that includes a given integer only if it is in three of the the four variables, if there are not three occurrences of an integer, then use 0.

M1    M2      M3      M4      M_NEW
1     1,2     0        1       1
3,4   3,4   1,2,3,4    4       3,4

我不知道如何处理这些逗号分隔的整数。如果它们是单个整数,我可以执行类似的操作:

I am unsure on how to deal with these comma separated integers. If they were single integers, I could do something like this:

# modified from http://stackoverflow.com/a/14114085/1670053
# over each row of data.frame (or matrix)
sapply(1:nrow(df), function(idx) {
# get the number of time each entry in df occurs
    t <- table(t(g[idx, ]))
# get the maximum count (or frequency)
    if(max(t) > 2){
      t.max <- max(t)
      }else{ t.max <- 0
    }
# get all values that equate to maximum count
    t <- as.numeric(names(t[t == t.max]))

})

虽然这些多个值用逗号分隔,但我不知道从哪里开始。 >

Though with these multiple values separated by commas, I am unsure where to start.

# data and example output
df <- structure(list(M1 = structure(c(3L, 2L, 2L, 5L, 3L, 1L, 7L, 1L, 
8L, 1L, 3L, 4L, 3L, 6L), .Label = c("0", "1", "1,2", "1,2,3", 
"1,2,3,4", "1,2,3,4,5", "3,4,5,6,7", "6,7,8,9,10,11,12,13,14,15,16"
), class = "factor"), M2 = structure(c(5L, 2L, 2L, 4L, 4L, 1L, 
11L, 8L, 7L, 9L, 3L, 6L, 3L, 10L), .Label = c("0", "1,2", "1,2,3", 
"1,2,3,4", "1,2,3,4,5", "1,2,3,4,5,6,7", "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16", 
"2", "2,3,4,5", "4,5,6", "4,5,6,7,8,9,10,11,12,13,14"), class = "factor"), 
    M3 = structure(c(4L, 1L, 1L, 8L, 3L, 1L, 6L, 1L, 7L, 3L, 
    2L, 5L, 9L, 3L), .Label = c("0", "1,2", "1,2,3,4", "1,2,3,4,5", 
    "1,2,3,4,5,6", "1,2,3,4,5,6,7,8", "1,2,3,4,5,6,7,8,9,10,11,12,13,14", 
    "3,4", "3,4,5"), class = "factor"), M4 = structure(c(5L, 
    1L, 2L, 8L, 2L, 1L, 6L, 3L, 4L, 1L, 3L, 3L, 7L, 9L), .Label = c("0", 
    "1", "1,2", "1,2,3,4,5,12,13,14,15,16,17", "1,2,3,4,5,6", 
    "1,2,3,4,5,6,7,8,9,10,11,12", "3,4", "4", "4,5"), class = "factor"), 
    M_NEW = structure(c(6L, 1L, 2L, 8L, 3L, 1L, 9L, 1L, 7L, 1L, 
    3L, 4L, 5L, 10L), .Label = c("0", "1", "1,2", "1,2,3", "1,2,3,", 
    "1,2,3,4,5", "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16", "3,4", 
    "3,4,5,6,7,8", "4,5"), class = "factor")), .Names = c("M1", 
"M2", "M3", "M4", "M_NEW"), class = "data.frame", row.names = c(NA, 
-14L))


推荐答案

f <- function(x, n=3) {
  tab <- table(strsplit(paste(x, collapse=","), ","))
  res <- paste(names(tab[which(tab >= n)]), collapse=",")
  return(ifelse(res == "", "0", res))
}
(df[, 5] <- apply(df[, 1:4], 1, f))
# [1] "1,2,3,4,5"                             
# [2] "0"                                     
# [3] "1"                                     
# [4] "3,4"                                   
# [5] "1,2"                                   
# [6] "0"                                     
# [7] "3,4,5,6,7,8"                           
# [8] "0"                                     
# [9] "1,10,11,12,13,14,15,16,2,3,4,5,6,7,8,9"
# [10] "0"                                     
# [11] "1,2"                                   
# [12] "1,2,3"                                 
# [13] "3"                                     
# [14] "4,5"  

这篇关于选择(多个)整数,每行出现n次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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