很抱歉,但是一个非常简单的问题。
我正在将python代码植入R。
这是与处理字符有关的非常简单的代码,但根本无法运行。
我需要您的帮助来更正我的代码。
谢谢。
Python代码
def hello(obj):
print("Hello" + obj +"!")
hello("cat")
以上代码的输出为
你好猫!
我在R中的代码
hello <- function(obj){
print("Hello ", obj, "!", quote=FALSE)
}
hello(cat)
预期结果
与Python相同,即
猫,你好!
实际结果
print.default(“ Hello”,obj,“!”,quote = FALSE)错误:无效的“数字”参数
答案 0 :(得分:0)
喜欢吗?
hello <- function(obj){
noquote(paste0("Hello ", obj, "!") )
}
hello("cat")
>[1] Hello cat!
如果要“漂亮打印”
hello <- function(obj){
cat(format(paste0("Hello ", obj, "!") ))
}
> hello("cat")
Hello cat!
答案 1 :(得分:0)
我们可以用paste
hello <- function(obj){
print(paste("Hello ", obj, "!", sep = ""), quote=FALSE)
#or use cat
# cat("Hello ", obj, "!")
}
hello("cat")
#[1] Hello cat!
注意:在评论中添加paste