Finding the length of each string within a column of a data-frame in R

sunitprasad1 picture sunitprasad1 · Mar 10, 2015 · Viewed 34.6k times · Source

I wish to calculate the number of characters of every string of the name column. My dataframe sample is as shown below :

date        name           expenditure      type
23MAR2013   KOSH ENTRP     4000             COMPANY
23MAR2013   JOHN DOE       800              INDIVIDUAL
24MAR2013   S KHAN         300              INDIVIDUAL
24MAR2013   JASINT PVT LTD 8000             COMPANY
25MAR2013   KOSH ENTRPRISE 2000             COMPANY
25MAR2013   JOHN S DOE     220              INDIVIDUAL
25MAR2013   S KHAN         300              INDIVIDUAL
26MAR2013   S KHAN         300              INDIVIDUAL

Why is that nchar giving me a list of random numbers? So is str_length() from stringr package

Length <- aggregate(nchar(sample$name), by=list(sample$name), FUN=nchar)

Output

         Group.1       x
1 JASINT PVT LTD       2
2       JOHN DOE       1
3     JOHN S DOE       2
4     KOSH ENTRP       2
5 KOSH ENTRPRISE       2
6         S KHAN 1, 1, 1

Desired Output :

     Group.1       x
1 JASINT PVT LTD       14
2       JOHN DOE       8
3     JOHN S DOE       10
4     KOSH ENTRP       10
5 KOSH ENTRPRISE       14
6         S KHAN       6

The csv for the above table :

"Date","name","expenditure","type"
"23MAR2013","KOSH ENTRP",4000,"COMPANY"
"23MAR2013 ","JOHN DOE",800,"INDIVIDUAL"
"24MAR2013","S KHAN",300,"INDIVIDUAL"
"24MAR2013","JASINT PVT LTD",8000,"COMPANY"
"25MAR2013","KOSH ENTRPRISE",2000,"COMPANY"
"25MAR2013","JOHN S DOE",220,"INDIVIDUAL"
"25MAR2013","S KHAN",300,"INDIVIDUAL"
"26MAR2013","S KHAN",300,"INDIVIDUAL"

Answer

xraynaud picture xraynaud · Mar 10, 2015

You can also apply nchar to your dataframe and get the result from the corresponding column:

data.frame(names=temp$name,chr=apply(temp,2,nchar)[,2])
      names chr
1     KOSH ENTRP  10
2       JOHN DOE   8
3         S KHAN   6
4 JASINT PVT LTD  14
5 KOSH ENTRPRISE  14
6     JOHN S DOE  10
7         S KHAN   6
8         S KHAN   6