简体   繁体   中英

How do I add a column to a data frame in R based on a partial string in another column?

I'm trying to create a new column with a returned value based on a partial string match in another column in R. The first column is a basketball team denoted by color and position (eg yellow_center, red_guard, orange_forward, etc). The column I'm trying to add is a team name column based on the color. In this example, I want the values Yellow, Orange, or Red returned based on the team/position string earlier in the row. There are only 4 or 5 colors I'll ever be using.

I've messed with grep, grepl, and transform functions but I'm not getting the expected answer.

I tried this to get the first letter, but it doesn't seem to be creating the column.

transform(teamfile, team.name <-substr(teamfile$player_position, 1)

I've tried grepl a few ways but didn't save them.

What I would like to see is a brand new column with "Yellow" or "Red" or "Orange" based on whatever color may be in the player_position column value.

If I understand you correctly you want to extract the color from the string, right? Using the stringr package you can use

library(stringr)
teamfile$teamname <- str_extract(teamfile$player_position,  "^.*(?=\\_)") 
# this regex extraxts everything up to a "_"
# so it extracts "orange" based on "orange_guard", "orange_center" or "orange_whatever"

df$new_column = unlist(strsplit(df$colname,'_'))[seq(1,nrow(df),2)] will split out the colors into its own column.

This will work. Using mtcars as an example dataset

mtcars$colour<- NA
for( i in c("Merc","Toyota","Ford","Mazda" )) {mtcars[ grepl( i  ,  rownames( mtcars), ) , "colour" ] <- i }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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