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 for循环的错误结果_R_Loops_For Loop - Fatal编程技术网

R for循环的错误结果

R for循环的错误结果,r,loops,for-loop,R,Loops,For Loop,我有下面的代码 n=c('a','b','c') one=c('a','c') two=c('b','a') three=data.frame(one, two) m=matrix(0,3,2) for (i in length(n) ) { m[i,]=t(1*(n[i]==three[,1])-1*(n[i]==three[,2])) } t(1*(n[1]==three[,1])-1*(n[1]==three[,2])) t(1*(n[2]==three[,1])-1*(n[2]=

我有下面的代码

n=c('a','b','c')
one=c('a','c')
two=c('b','a')
three=data.frame(one, two)
m=matrix(0,3,2)
for (i in length(n) ) {
   m[i,]=t(1*(n[i]==three[,1])-1*(n[i]==three[,2]))
}

t(1*(n[1]==three[,1])-1*(n[1]==three[,2]))
t(1*(n[2]==three[,1])-1*(n[2]==three[,2]))
t(1*(n[3]==three[,1])-1*(n[3]==three[,2]))
为什么m矩阵的输出与最后3行的输出不同?有什么有效的方法可以做到这一点吗?

因为你想

for (i in seq_along(n)) {
因为你想要

for (i in seq_along(n)) {
因为你想要

for (i in seq_along(n)) {
因为你想要

for (i in seq_along(n)) {

既然您问是否有更好的方法使用
apply
函数来实现这一点,那就来吧。
do.call(rbind,…)
的结果“自然”强制为矩阵,因此无需事先定义矩阵
m

我不明白乘以1背后的逻辑,所以我把它省略了。如果你需要它,它仍然可以工作

> n <- c('a','b','c')
> three <- data.frame(one = c("a", "c"), two = c("b", "a"))
> m <- do.call(rbind, lapply(seq(n), function(i){
+     t((n[i] == three[,1]) - (n[i] == three[,2]))
+ }))
> m
     [,1] [,2]
[1,]    1   -1
[2,]   -1    0
[3,]    0    1
>n三米
[,1] [,2]
[1,]    1   -1
[2,]   -1    0
[3,]    0    1

既然您询问是否有更好的方法使用
apply
函数来实现这一点,那就来吧。
do.call(rbind,…)
的结果“自然”强制为矩阵,因此无需事先定义矩阵
m

我不明白乘以1背后的逻辑,所以我把它省略了。如果你需要它,它仍然可以工作

> n <- c('a','b','c')
> three <- data.frame(one = c("a", "c"), two = c("b", "a"))
> m <- do.call(rbind, lapply(seq(n), function(i){
+     t((n[i] == three[,1]) - (n[i] == three[,2]))
+ }))
> m
     [,1] [,2]
[1,]    1   -1
[2,]   -1    0
[3,]    0    1
>n三米
[,1] [,2]
[1,]    1   -1
[2,]   -1    0
[3,]    0    1

既然您询问是否有更好的方法使用
apply
函数来实现这一点,那就来吧。
do.call(rbind,…)
的结果“自然”强制为矩阵,因此无需事先定义矩阵
m

我不明白乘以1背后的逻辑,所以我把它省略了。如果你需要它,它仍然可以工作

> n <- c('a','b','c')
> three <- data.frame(one = c("a", "c"), two = c("b", "a"))
> m <- do.call(rbind, lapply(seq(n), function(i){
+     t((n[i] == three[,1]) - (n[i] == three[,2]))
+ }))
> m
     [,1] [,2]
[1,]    1   -1
[2,]   -1    0
[3,]    0    1
>n三米
[,1] [,2]
[1,]    1   -1
[2,]   -1    0
[3,]    0    1

既然您询问是否有更好的方法使用
apply
函数来实现这一点,那就来吧。
do.call(rbind,…)
的结果“自然”强制为矩阵,因此无需事先定义矩阵
m

我不明白乘以1背后的逻辑,所以我把它省略了。如果你需要它,它仍然可以工作

> n <- c('a','b','c')
> three <- data.frame(one = c("a", "c"), two = c("b", "a"))
> m <- do.call(rbind, lapply(seq(n), function(i){
+     t((n[i] == three[,1]) - (n[i] == three[,2]))
+ }))
> m
     [,1] [,2]
[1,]    1   -1
[2,]   -1    0
[3,]    0    1
>n三米
[,1] [,2]
[1,]    1   -1
[2,]   -1    0
[3,]    0    1

i in 1:length(n)
i in seq(length=n)
是否有更好的方法使用apply函数执行此操作?是的,有。请参阅我在1:length(n)中的答案
i
或在seq(length=n)中的答案
i
是否有更好的方法使用apply函数执行此操作?是的,有。请参阅我在1:length(n)中的答案
i
或在seq(length=n)中的答案
i
是否有更好的方法使用apply函数执行此操作?是的,有。请参阅我在1:length(n)中的答案
i
或在seq(length=n)中的答案
i
是否有更好的方法使用apply函数执行此操作?是的,有。请参阅我的回答是否有更好的方法使用应用函数?是否有更好的方法使用应用函数?是否有更好的方法使用应用函数?是否有更好的方法使用应用函数?