roshan roshan - 1 month ago 13
R Question

Sub-setting words in data frame with specific length of word

I have data frame like

description
csi
ssi
o
of
in
over
mss
improvement
channel


I want to extract words having length 1-2 and result should be a data frame having following output:

description
o
of
in


Can any one please help using R-programming

Answer

We can apply the nchar on the 'description' column to create a logical index for subsetting the rows

df1[nchar(df1$description)<3,, drop = FALSE]
#     description
#3           o
#4          of
#5          in

Or use subset so that by default it is drop = FALSE

subset(df1, nchar(description) < 3)