2015-02-24 70 views
-1

假设我正在读取R的列中包含0和1的字符串的.csv文件。假设我需要比较1的位置,如果匹配,则每个匹配计数为1,并将该计数放在第三列中。比较1的位置在r中的字符串中匹配

插图

dput(head(string_data)) 
structure(list(v_1 = structure(c(1L, 1L, 1L, 1L, 3L, 1L), .Label = c("", 
"0,0,0,1", "0,0,1,0", "0,1,0,0", "1,1,0,0"), class = "factor"), 
    v_2 = structure(c(1L, 1L, 1L, 1L, 2L, 1L), .Label = c("", 
    "1,0,1,0"), class = "factor"), v_3 = structure(c(1L, 1L, 
    1L, 1L, 4L, 1L), .Label = c("", "0,0,0,1", "0,0,1,0", "1,0,0,0" 
    ), class = "factor"), v_4 = structure(c(1L, 1L, 1L, 1L, 2L, 
    1L), .Label = c("", "0,0,0,1"), class = "factor"), v_5 = structure(c(1L, 
    5L, 1L, 1L, 1L, 2L), .Label = c("", "0,0,0,0,0", "0,0,0,1,0", 
    "0,0,1,0,0", "1,0,1,1,0"), class = "factor"), v_6 = structure(c(1L, 
    2L, 1L, 1L, 1L, 2L), .Label = c("", "1,0,1,1,0"), class = "factor"), 
    v_7 = structure(c(1L, 1L, 1L, 1L, 1L, 2L), .Label = c("", 
    "0,0,0,0", "0,0,0,1", "0,1,0,0", "1,0,0,0"), class = "factor"), 
    v_8 = structure(c(1L, 1L, 1L, 1L, 1L, 2L), .Label = c("", 
    "1,0,0,0"), class = "factor")), .Names = c("v_1", "v_2", 
"v_3", "v_4", "v_5", "v_6", "v_7", "v_8"), row.names = c(NA, 
6L), class = "data.frame") 

上面我已经贴头数据的dput

我需要比较(2 * i-1)列中第1列的位置与第(2 * i)列(i = 1,2,...,8)并将其放在第三列中。作为比赛数量。

例如

假设我有在第一列在第二列中输入字符串0,0,1,1和0,1,1,1然后在第三列,它应该返回2.

任何人都可以请帮我与这一个。

EDIT

在第三列中的计数应当基于1周的的在第二列的字符串的数目。在上述例如第二列的字符串是0,1,1,1这意味着它的计数可以非常从0到3

+0

这是否太模糊的问题?还是很难? – Artiga 2015-02-24 06:17:35

+2

请提供您的示例的预期输出。 – 2015-02-24 07:42:40

回答

0

这对夫妻的功能可能是有帮助作为起动:

# Compares two strings and computes number of '1's at matching positions 
f <- function(s1, s2) { 
    if (s1=='' || s2=='') return(0) 
    m <- do.call(cbind,strsplit(c(s1,s2),',')) 
    m2 <- rowMeans(m=="1") 
    sum(m2==1.0) 
} 

# Calls `f()` for every row of two columns i and j from a data set d and returns a vector 
# that could be used as a new column 
f.cols <- function(d,i,j) { 
    c1 <- as.character(d[,i]) 
    c2 <- as.character(d[,j]) 
    unname(mapply(f,c1,c2)) 
} 

使用的示例:

d$out <- f.cols(d,1,2) 
相关问题