R:我可以更新 magrittr 管道中对象的类吗? [英] R: can I update the class of a an object in a magrittr pipe?
问题描述
我有一段代码用于更新对象的类.但是我必须打破代码的跟随才能分配课程.是否有一种优雅的方式来分配课程但继续管道,所以我有一个管道一直到最终结果?我怀疑 {purrr} 中可能有什么东西?
I have a piece of code where I update the class of the object. But I have to break the follow of the code to assign the class. Is there an elegant to to assign the class but continue the pipe so I have one pipe all the way to the final result? I suspect there might be something in {purrr}?
library(disk.frame)
library(dplyr)
library(tidyquery)
a = nycflights13::airports %>%
as.disk.frame
class(a) <- c(class(a), "data.frame")
a %>%
query("SELECT name, lat, lon ORDER BY lat DESC LIMIT 5")
推荐答案
当然,你可以使用 "class<-"()
:
library(dplyr)
x <- 1:10 %>%
"class<-"("foo")
x
# [1] 1 2 3 4 5 6 7 8 9 10
# attr(,"class")
# [1] "foo"
详情
通常,在 R 中,当您可以分配给函数的输出时,例如class(x) <- "foo"
,你使用的是替换函数",例如"class<-"()
.可以在此处找到有关 Stack Overflow 上对此的详细讨论.
Details
Generally, in R, when you can assign to a function's output, e.g. class(x) <- "foo"
, what you're using is a "replacement function", e.g. "class<-"()
. A good discussion of this on Stack Overflow can be found here.
这篇关于R:我可以更新 magrittr 管道中对象的类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!