r - Adding line breaks to concatenating functions? -
what command have use adding linebreaks while concatenating functions?
here script:
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") 1 <- function(x){ x <- tolower(x) # assuming row names in lower case myrow <- fruit[x,] count <- sapply(seq_along(myrow), function(x, n, i) {paste0(x[i], "")}, x=myrow[1], n=names(myrow)) count[length(count)] <- paste0(count[length(count)]) count <- paste(count[1]) cat(tools::totitlecase(x), "has", count, "thousand farms") # general statement }
here i've tried:
cat(one("canada"), '\n\n', one("canada")) canada has 50 thousand farmscanada has 50 thousand farms
i want this:
canada has 50 thousand farms canada has 50 thousand farms
your problem within funciton use cat should use paste (check ?paste , ?cat understand differences). funciton should work fine:
one <- function(x){ x <- tolower(x) # assuming row names in lower case myrow <- fruit[x,] count <- sapply(seq_along(myrow), function(x, n, i) {paste0(x[i], "")}, x=myrow[1], n=names(myrow)) count[length(count)] <- paste0(count[length(count)]) count <- paste(count[1]) ret <- paste(tools::totitlecase(x), "has", count, "thousand farms") # general statement }
in order remove trailing blanks second line, have add sep="" last statement:
cat(one("canada"), ' \n\n ', one("canada"), sep="")
Comments
Post a Comment