Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substring of all data frame elements

Tags:

substring

r

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)
like image 971
SabreWolfy Avatar asked Jan 22 '14 12:01

SabreWolfy


People also ask

How do you find the substring of a data frame?

Using “contains” to Find a Substring in a Pandas DataFrame The contains method in Pandas allows you to search a column for a specific substring. The contains method returns boolean values for the Series with True for if the original Series value contains the substring and False if not.

How do I cut all values of a column in pandas?

Method 3: Slice Columns in pandas using iloc[] The iloc is present in the Pandas package. The iloc can be used to slice a Dataframe using indexing. df. iloc[] method is used when the index label of a data frame is something other than numeric series of 0, 1, 2, 3….


1 Answers

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) 
like image 177
lukeA Avatar answered Sep 29 '22 16:09

lukeA