Ch_soil
Ch_soil

Reputation: 13

Fill new column using only part of the content in another column

I have a column called "ID" in my dataframe which consist of sample names such as: C1, C2, C3,...C20,O1,..., O20.

Now I would like to create a new column called "treatment" where I fill the word "Conventional" for all the cases where there was a "C" in front of the number in "ID" and "Organic" where there was an "O" before the name.

Example

ID      treatment
C1      conventional
C2      conventional
O1      organic

I found a similar question here but there they used the whole content and not just part of the content as I would need: Aggregate data in one column based on values in another column

Upvotes: 1

Views: 94

Answers (3)

sorearm
sorearm

Reputation: 409

Try using the tidyverse nomenclature too, there's some very powerful easy-to-follow commands in there and you don't fall foul of the if_else issues where you can easily get lost;

ID <- c('C1', 'C2', 'O1', 'CG18', 'OG20')
dat <- data.frame(ID)

require(tidyverse)

# use 'case_when' from dplyr and 'stringr' commands
dat <- dat %>% 
  mutate(
    treatment = case_when(

      # regex anchors 'C' at beginning followed by digits
      str_detect(ID, '^C\\d+') ~ 'conventional'  

      # regex anchors 'O' at beginning followed by digits
      , str_detect(ID, '^O\\d+') ~ 'organic'

      # regex to detect 'G'
      , str_detect(ID, 'G') ~ 'grassland'
      , TRUE ~ 'NA')
  )

should give you this

Upvotes: 1

moodymudskipper
moodymudskipper

Reputation: 47300

You could also do it this way :

df <- data.frame(ID = c("C1","C2","O1"))
lkp <- c(C = 'conventional', O = 'organic')
# or to answer your comment on other answer :
# lkp <- c(C = 'conventional', O = 'organic', CG = 'grassland', OG = 'grassland')
df$treatment <- lkp[gsub("\\d.*","",df$ID)]
df
#   ID    treatment
# 1 C1 conventional
# 2 C2 conventional
# 3 O1      organic

Upvotes: 0

user2974951
user2974951

Reputation: 10375

Something like

mydf$treatment=ifelse(substr(mydf$ID,1,1)=="C","Conventional","Organic")

?

Upvotes: 1

Related Questions