SabreWolfy
SabreWolfy

Reputation: 5540

Substring of all data frame elements

Consider:

x = data.frame(c('ABCD', 'EFGH'), row.names=c('1A', '1B'))

I need a substring of each element in the data frame. Something like this:

substring(x, 2,4)

Upvotes: 7

Views: 15193

Answers (1)

lukeA
lukeA

Reputation: 54285

You could use sapply:

sapply(x, substring, 2, 4)

or, if you want to take only one specific column - say #1 - of the data frame:

substring(x[,1], 2, 4) 

Upvotes: 14

Related Questions