0

I am trying to get rid of the first three characters of a string variable only for the first 18 observations (rows). How can I do this? I tried the code below but got an error: Error in parse(text = x) : :1:3: unexpected symbol

condition <- substring(Attend$"sample_label", 3) 
  filter_(Attend,condition) %>% slice(1:18)
Nader Mehri
  • 514
  • 1
  • 5
  • 21

2 Answers2

1

please include more details. If I understand correctly, your dataframe is called Attend and the column of interest sample_label. If so, first of all, you do not need the quote marks (Attend$sample_label instead of Attend$"sample_label"). An easy way to do what you wish is the following:

library(dplyr)

Attend[1:18,]<-Attend[1:18,] %>%
                mutate(sample_label=substring(sample_label, first=4))
0

We can use sub to remove first 3 characters for first 18 values in sample_label column.

Attend$sample_label[1:18] <- sub('.{3}', '', Attend$sample_label[1:18])
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213