简体   繁体   English

在 R dataframe 中创建一个新的 class

[英]Creating a new class in R dataframe

I am in need of creating a new class in R dataframe. Example,我需要在 R dataframe 中创建一个新的 class。例如,

asd <- data.frame(a = c("A", "B"), b = c("D","S"))
class(asd$b) <- "New" 

As you see, I have a created a new class "New".如您所见,我创建了一个新的 class“New”。 But when I do below operation, I do not get a但是当我进行以下操作时,我没有得到

asd %>% select_if(is.New) 
Error in is_logical(.predicate) : object 'is.New' not found

Expected output预计 output

   b
1  D
2  S

Just because you create a class of "New" doesn't mean a function with the name is.New was also created.仅仅因为您创建了“New”的 class 并不意味着还创建了名为is.New的 function。 The generic form of is() takes a class name as a character value. is()的通用形式采用 class 名称作为字符值。 You would use it like你会像这样使用它

asd %>% select_if(~is(., "New"))

And if you wanted to create is.New you could do如果你想创建is.New你可以做

is.New <- function(x) is(x, "New")

asd %>% select_if(is.New) 

We may use select with where我们可以where使用select

library(dplyr)
asd %>%
    select(where(~ is(., "New")))

-output -输出

 b
1 D
2 S

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM