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

r add column to dataframe based on other columns
grepl in r
r add column with values based on another column
r data table add column based on other columns
if column contains value then r
add column to dataframe r
add multiple columns to data frame r
r create new column based on condition

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 (e.g. 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"

How to add a column in one dataframe based from a partial string , How is the row order of the first dataframe matched to the second dataframe? How do I add a column in one dataframe based from a partial string match from another dataframe in R? Where “xxxxxx” is an R expression that creates the data. get a match, before add the one from other data frame can be one approach. I found this: Add column to Data Frame based on values of other columns, but it was'nt exactly what I need in my specific case. I'm really a beginner in R, so I hope maybe someone can help me or has a good hint for me. Here an example of what my data frame looks like:


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

8.4 Dataframe column names, How do I add a column to a Dataframe in R? DataFrame [, ‘new_column_name’] <- Column_To_Add. Another way for us to add a column to an existing data frame in R is to use the syntax: DataFrame [, 'new_column_name'] <- Column_To_Add. 1. DataFrame[, 'new_column_name'] <- Column_To_Add. For example, df [, 'reviews'] <- reviews. 1. df[, 'reviews'] <- reviews.


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 }

A Beginner Guide to String Pattern Matching in R by Regular , using the $ and assignment <- operators. To do this, just use the df$name notation and assign a new vector of data to it. We can add multiple variables/columns to a data frame using cbind () function. To add the multiple columns to a data frame we need to follow the below steps. Create a new Data Frame with an individual column using vector c () function. Use the cbind () function to add a new data frame as the variables.


R grepl Function Examples -- EndMemo, element values, set the option value to TRUE by value=TRUE . A common data manipulation task in R involves merging to data frames together. One of the simplest ways to do this is with the cbind function. The cbind function – short for column bind – can be used to combine two data frames with the same number of rows into a single data frame.


Merge Data Frames in R: Full and Partial Match, () function searchs for matches of a string or string vector. It returns TRUE if a string contains the pattern, otherwise FALSE; if the parameter is a string vector, returns a logical vector (match or not for each element of the vector). It stands for "grep logical". We can see that the column “chol” was sorted in reserving order compared to above example. 5.3. Order A Data Frame By Multiple Columns. To order a data frame by multiple columns using the orderBy function of the doBy package, we will need to create a formula which combines the desired columns with sorting orders, for example, let’s sort the data frame by both the columns “chol” and


fuzzyjoin package, Column x to merge on -by.y: The column used for merging in y data We add stringsAsFactors=FALSE in the data frame because we don't Let's merge data frames when the common key variables have different names. For example I have the following data frame. What I want to do is to add another column (7th column) in to that data frame. The condition is if Sepal.Length >=5 assign "UP" else assign "DOWN". We'd call that column "Regulation".