我有一个函数,它接受单元格值并从它们生成注释。
我正在努力学习如何有效地使用应用程序。
我传入一个数据框架,其中包含10行和4列,目的是根据行的每个单元格中包含的值,在每行中生成一个句子。
report <- iris
build_comments <- function (report) {
mydf <- report
mycomment <- paste("Analysis Of", mydf[,5],"for flower", mydf[,5], "with width", mydf[,2], "being the most impacted with ",
"Most of the items had length of", mydf[,1],
sep = " ")
cat(mycomment)
}
apply(report[,c(2,3,10,11)],1,build_comments)不幸的是,它正在构建一个单独的评论,列中的每一项都添加到了位置持有者。有人能看到错误吗?
如果您认为比较容易,我也会采用purrr方法。
发布于 2016-10-24 16:46:25
试试这个例子:
mtcars$comment <-
with(mtcars,
paste("This car has", cyl, "cylinders",
"and", gear, "gears."))或者如罗兰所建议的,使用sprintf()
mtcars$comment <-
with(mtcars,
sprintf("This car has %i cylinders and %i gears.",
cyl, gear))with()只需计算data.frame中的表达式,就可以避免重复输入mtcars$。
# without using "with()"
mtcars$comment <-
sprintf("This car has %i cylinders and %i gears.",
mtcars$cyl, mtcars$gear)https://stackoverflow.com/questions/40220844
复制相似问题