英文:
Failure in Calling a Function in R
问题
我正在尝试创建一个比较两个矩阵的函数。它将比较两个矩阵在特定位置的元素,并返回"greater than"(大于)"equal to"(等于)或"less than"(小于)。以下是我目前的代码。然而,当我尝试调用这个函数时,R没有返回任何内容,甚至没有错误消息。我想知道为什么会这样。任何建议将非常有帮助。谢谢。
fxn <- function(x, y) {
emptymatrix <- matrix( , nrow = dim(x)[1], ncol = dim(x)[2])
for (i in 1:dim(emptymatrix)[1]) {
for (j in 1:dim(emptymatrix)[2]) {
if (x[i, j] < y[i, j]) {
emptymatrix[i, j] <- "Less Than"
}else if (x[i, j] == y[i, j]) {
emptymatrix[i, j] <- "Equal to"
}else {
emptymatrix[i, j] <- "Greater than"
}
}
}
}
#尝试测试函数
vecc1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
vecc2 <- c(4, 5, 2, 3, 1, 1, 8, 9, 10)
matrix1 <- matrix(vecc1, nrow = 3, byrow = T)
matrix2 <- matrix (vecc2, nrow=3, byrow = T)
fxn(matrix1, matrix2)
英文:
I'm trying to create a function that compares two matrices. It will compare the element of both matrices at a certain position, and returns "greater than" "equal to" or "less than". Below is the code I have right now. However, when I tried calling the function, R does not return anything, not even an error message. I'm wondering why that is the case. Any suggestions would be helpful. Thanks.
fxn <- function(x, y) {
emptymatrix <- matrix( , nrow = dim(x)[1], ncol = dim(x)[2])
for (i in 1:dim(emptymatrix)[1]) {
for (j in 1:dim(emptymatrix)[2]) {
if (x[i, j] < y[i, j]) {
emptymatrix[i, j] <- "Less Than"
}else if (x[i, j] == y[i, j]) {
emptymatrix[i, j] <- "Equal to"
}else {
emptymatrix[i, j] <- "Greater than"
}
}
}
}
#trying to test the function
vecc1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
vecc2 <- c(4, 5, 2, 3, 1, 1, 8, 9, 10)
matrix1 <- matrix(vecc1, nrow = 3, byrow = T)
matrix2 <- matrix (vecc2, nrow=3, byrow = T)
fxn(matrix1, matrix2)
答案1
得分: 3
你好,正如SamR在他的评论中指出的,你的函数没有返回任何东西,因为它最后没有返回函数/对象。他也在循环方面是正确的,因为R主要设计用于表格数据和矩阵,它可以在幕后为你做很多事情。这是R具有的一些设计原则的很好的例子。首先,我们不需要使用for循环,因为我们可以在所有索引上进行大于等于小于的评估(向量化)。输出将是一个大小为M的矩阵,包含TRUE / FALSE。我们可以使用这个矩阵来索引我们的新矩阵的所有TRUE位置。然后我们只需要分配一个单一的字符串"equal"、"larger"或"less",它会被循环到较长的向量/列表的长度。
vecc1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
vecc2 <- c(4, 5, 2, 3, 1, 1, 8, 9, 10)
matrix1 <- matrix(vecc1, nrow = 3, byrow = T)
matrix2 <- matrix(vecc2, nrow = 3, byrow = T)
# 运行这个代码查看比较的结果
matrix1 == matrix2
foo <- function(x, y) {
m_new <- matrix(NA, nrow = dim(x), ncol = dim(x))
m_new[x == y] <- "Equal"
m_new[x < y] <- "Less Than"
m_new[x > y] <- "Greater Than"
m_new # 更快
# return(m_new) 不如这样效率高
}
foo(matrix1, matrix2)
希望这个翻译对你有所帮助。
英文:
Hi as SamR pointed out in his comment, your function doesn't return anything, because it has no return function / object in the end. He is also right about the loop thing, because R is mainly designed for tabular data and matrices it can do a lot of stuff for you under the hood. This is a great examples about some design principles R has. First we don't need to use a for loop because we can just evaluate larger equal less, on all indices (vectorized). The output will be a matrix of size M with TRUE / FALSE. we can use this matrix to index our new matrix at all TRUE position. than we just need to assign a single string "equal", "larger", or "less" that gets recycled to the length of the longer vector(/list).
vecc1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
vecc2 <- c(4, 5, 2, 3, 1, 1, 8, 9, 10)
matrix1 <- matrix(vecc1, nrow = 3, byrow = T)
matrix2 <- matrix (vecc2, nrow=3, byrow = T)
# run this to see how the comparision works
matrix1 == matrix2
foo <- function(x,y) {
m_new<-matrix(NA,nrow=dim(x),ncol=dim(x))
m_new[x==y]<-"Equal"
m_new[x<y]<-"Less Than"
m_new[x>y]<-"Greater Than"
m_new # faster
#return(m_new) is not as efficent
}
foo(matrix1,matrix2)
答案2
得分: 0
你没有从你的函数中返回emptyMatrix
。
在R中,函数中最后一个语句的结果会自动返回。在原始函数中,最后一个语句是for
循环,其值为NULL。它被返回,标记为“invisible”,所以没有打印出来。
在R中通常的约定是,当要返回的对象不是已经产生的最后一个值时,要添加一行包含emptyMatrix
的语句。
你也可以调用return(emptyMatrix)
,但实际上这会更不高效。
如果你喜欢像for
循环一样以不可见方式返回东西,你可以在最后一行调用invisible(emptyMatrix)
。然后它就不会自动打印出来,但你仍然可以将它赋给另一个变量。
英文:
You missed returning emptyMatrix
from your function.
In R, the result of the last statement in a function is returned automatically. In the original function, the last statement was the for
loop, whose value is NULL. It was returned, marked "invisible", so it didn't print.
The usual convention in R is to type the name of the object you want to return when it isn't already the last value produced. So just add one line to your function, containing emptyMatrix
.
You can also call return(emptyMatrix)
, but that's actually less efficient.
And if you like returning things invisibly like for
loops do, you can call invisible(emptyMatrix)
as the last line. Then it won't automatically print, but you can still assign it to another variable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论