Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
r中的自设计矩阵_R_Loops_Matrix - Fatal编程技术网

r中的自设计矩阵

r中的自设计矩阵,r,loops,matrix,R,Loops,Matrix,以下是我的数据: sub <- paste ("s", 1:6, sep = "") mark1a <- c("A", "A", "B", "d1", "A", 2) mark1b <- c("A", "B", "d1", 2, "d1", "A") myd <- data.frame (sub, mark1a, mark1b) myd sub mark1a mark1b 1 s1 A A 2 s2 A B 3 s

以下是我的数据:

sub <- paste ("s", 1:6, sep = "")
mark1a <- c("A", "A", "B", "d1", "A", 2)
mark1b <- c("A", "B", "d1", 2, "d1", "A")
myd <- data.frame (sub, mark1a, mark1b)
myd 
     sub mark1a mark1b
1  s1      A      A
2  s2      A      B
3  s3      B     d1
4  s4     d1      2
5  s5      A     d1
6  s6      2      A

sub您可以尝试以下方法:

cbind(myd, t(apply(myd, 1, function(x) sapply(unique(unlist(myd[, 2:3])), function(y) sum(x==y)))))
1  s1      A      A 2 0  0 0
2  s2      A      B 1 1  0 0
3  s3      B     d1 0 1  1 0
4  s4     d1      2 0 0  1 1
5  s5      A     d1 1 0  1 0
6  s6      2      A 1 0  0 1

我想说@jmsigner的解决方案是单行程序的解决方案,但我通常会被那些嵌套的
apply
(及其相关)解决方案弄糊涂

这里有一个类似的解决方案:

# Identify all the levels in `mark1a` and `mark1b`
mydLevels = unique(c(levels(myd$mark1a), levels(myd$mark1b)))
# Use these levels and an anonymous function with `lapply`
temp = data.frame(lapply(mydLevels, 
                         function(x) rowSums(myd[-1] == x)+0))
colnames(temp) = mydLevels
# This gives you the correct output, but not in the order
# that you have in your original question.
cbind(myd, temp)
#   sub mark1a mark1b 2 A B d1
# 1  s1      A      A 0 2 0  0
# 2  s2      A      B 0 1 1  0
# 3  s3      B     d1 0 0 1  1
# 4  s4     d1      2 1 0 0  1
# 5  s5      A     d1 0 1 0  1
# 6  s6      2      A 1 1 0  0

首先,确保
mark1a
mark1b
列共享相同的级别:

all.levels <- levels(myd["mark1a", "mark1b"])
levels(myd$mark1a) <- all.levels
levels(myd$mark1b) <- all.levels

此外,您可能对
paste0
函数感兴趣。使用“表格”结果进行矩阵加法。我喜欢。我怎么知道级别的顺序(这里没有显示,是按字母顺序排列的吗
library(plyr)
cbind(myd, ddply(myd, "sub", function(x)table(x$mark1a) + table(x$mark1b))[,-1])
#   sub mark1a mark1b 2 A B d1
# 1  s1      A      A 0 2 0  0
# 2  s2      A      B 0 1 1  0
# 3  s3      B     d1 0 0 1  1
# 4  s4     d1      2 1 0 0  1
# 5  s5      A     d1 0 1 0  1
# 6  s6      2      A 1 1 0  0