Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"
like image 818
sunitprasad1 Avatar asked Dec 20 '22 06:12

sunitprasad1


2 Answers

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
like image 99
xraynaud Avatar answered Jan 27 '23 07:01

xraynaud


If the last row in "Desired Output" is a typo,

 aggregate(name~name1, transform(sample, name1=name),
                         FUN=function(x) nchar(unique(x)))
 #            name1 name
 #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

Or

 Un1 <- unique(sample$name)
 data.frame(Group=Un1, x=nchar(Un1))
like image 27
akrun Avatar answered Jan 27 '23 07:01

akrun