我有以下代码:
get_context_data
我希望它看起来像这样:
s complement and two
这就是我的尝试:
canada <- c(50, 50, 50)
korea <- c(70, 70, 70)
brazil <- c(100, 100, 100)
fruit <- rbind(canada, korea, brazil)
colnames(fruit) <- c("apple", "orange", "banana")
但是,这就是我得到的:
There are 50 thousand farms in Canada.
我需要保留我在这里演示过的代码。到目前为止,我尝试了粘贴功能,但我知道 paste0 应该是我用来摆脱最后空间的东西。非常感谢您的帮助。
答案 0 :(得分:1)
对字符串部分使用paste0
而不是计数。并手动编写需要的空格。
像这样:
one <- function(x){
x <- tolower(x) # assuming all row names are in lower case
myrow <- fruit[x,]
country <- paste0(tools::toTitleCase(x))
count <- sapply(seq_along(myrow),
function(x, n, i){paste0(x)},
x=myrow[1], n=names(myrow))
count[length(count)] <- paste0(count[length(count)])
count <- count[1]
cat(paste0("There are ", count, " thousand farms in ", country, "."))
}
one("canada")
答案 1 :(得分:0)
我不是程序员,所以我不太清楚语法。但在Python中,这可以修复如下
one <- function(x){
x <- tolower(x) # assuming all row names are in lower case
myrow <- fruit[x,]
country <- paste0(tools::toTitleCase(x))
count <- sapply(seq_along(myrow),
function(x, n, i){paste0(x)},
x=myrow[1], n=names(myrow))
count[length(count)] <- paste0(count[length(count)])
count <- paste0(count[1])
cat("There are "+ str(count) " thousand farms in "+ country + ".")
}
cat(one("canada"))
我在这里尝试做的不是,
而是使用+
和str(count)
的连接来将count
的整数值更改为字符串值。< / p>
答案 2 :(得分:0)
也许我完全不在,但以下情况相同,而且更简单。
one <- function(x){
x <- tolower(x) # assuming all row names are in lower case
count <- fruit[x, 1]
country <- paste0(tools::toTitleCase(x))
cat(paste0("There are ", count, " thousand farms in ", country, ".\n"))
}
one("canada")
There are 50 thousand farms in Canada.
one("korea")
There are 70 thousand farms in Korea.
one("brazil")
There are 100 thousand farms in Brazil.
如果我不理解,请原谅噪音。