Replacing values from a column using a condition in R

后端 未结 2 930
迷失自我
迷失自我 2020-11-28 06:19

I have a very basic R question but I am having a hard time trying to get the right answer. I have a data frame that looks like this:

 ind<-re         


        
相关标签:
2条回答
  • 2020-11-28 06:29

    I arrived here from a google search, since my other code is 'tidy' so leaving the 'tidy' way for anyone who else who may find it useful

    library(dplyr)
    iris %>% 
      mutate(Species = ifelse(as.character(Species) == "virginica", "newValue", as.character(Species)))
    
    
    0 讨论(0)
  • 2020-11-28 06:31
    # reassign depth values under 10 to zero
    df$depth[df$depth<10] <- 0
    

    (For the columns that are factors, you can only assign values that are factor levels. If you wanted to assign a value that wasn't currently a factor level, you would need to create the additional level first:

    levels(df$species) <- c(levels(df$species), "unknown") 
    df$species[df$depth<10]  <- "unknown" 
    
    0 讨论(0)
提交回复
热议问题