1

I have a data set that contains a column called calories but all the values start with a "[". We want to do numerical analysis, but the [ is impeding that. We've tried with both gsub and regex.

Currently, our data looks like this:

Calories

[235

[456

[876

And we would like to make it look like this:

Calories

235

456

876

Many thanks!

1 Answers1

2

Try substr or gsub:

df$Calories <- substr(df$Calories,2,nchar(df$Calories))

or

df$Calories <- gsub("^\\[(.*)","\\1",df$Calories)

such that

> df
  Calories
1      235
2      456
3      876

Data

df <- structure(list(Calories = c("[235", "[456", "[876")), class = "data.frame", row.names = c(NA, 
-3L))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81