String as factor in R

前端 未结 2 2013
离开以前
离开以前 2021-01-12 17:02

When creating a data frame in R, strings are by default converted to factors (which I don\'t mind). But when I want to create a new row to my data frame, I can\'t find a way

相关标签:
2条回答
  • 2021-01-12 17:35

    Maybe something like this is going to help you?

    data.frame(c("Name one", "Name two")) -> my.data
    colnames(my.data) <- "Names"
    
    rbind(my.data, data.frame(Names="name three"))
    
    0 讨论(0)
  • 2021-01-12 17:36

    The trick is to only rbind a data.frame with another data.frame and not, as you have done, with a simple vector:

    my.data <- data.frame(Names = c("Name one", "Name two"))
    new.row1 <- data.frame(Names = c("Name three"))
    rbind(my.data, new.row1)
    
    ##        Names
    ## 1   Name one
    ## 2   Name two
    ## 3 Name three
    
    0 讨论(0)
提交回复
热议问题