Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the first three characters from every row in a column in R

I have a large data set with a column of text, 20K rows. Would like to remove the first x number (e.g. 3) of characters at the beginning of each row in that specific column. Appreciate your assistance.

like image 674
Shawn Avatar asked Dec 02 '22 10:12

Shawn


1 Answers

You can do it with gsub function and simple regex. Here is the code:

# Fake data frame
df <- data.frame(text_col = c("abcd", "abcde", "abcdef"))
df$text_col <- as.character(df$text_col)

# Replace first 3 chracters with empty string ""
df$text_col <- gsub("^.{0,3}", "", df$text_col)
like image 192
Istrel Avatar answered Jan 11 '23 10:01

Istrel