-2

I have a data set which has a single column containing multiple names. For eg

  • Alex
  • Brad
  • Chrisitne
  • Alexa
  • Brandone

And almost 100 records like this. I want to display record as

  • A 2
  • B 2
  • C 1

Which means i need to show this frequency from higher to lower and if there is a tie breaker , the the values should be shown in Alphabetical Order . I have been trying to solve this but i am not able to. Any pointer on these ?

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
S_Gupta
  • 1
  • 3
  • 2
    Welcome to SO. Is this a plain text file with one name on each line or are there dashes like you used in the editor pane when you posted the question? What code did you use to try to read in the file (this isn't a code writing service)? What R string functions did you research that might be used to get substrings out of a string? Please show some code. – hrbrmstr Nov 11 '18 at 11:39
  • It is a CSV file. – S_Gupta Nov 11 '18 at 11:40
  • There are no Dashes . Just a simple name , I tried using Substring function but didnt help – S_Gupta Nov 11 '18 at 11:41
  • I am new to this so can you please help me with the code sample of how to do ths – S_Gupta Nov 11 '18 at 11:41
  • 1
    Even if it does not helped put the code in your question! Read [ask] and https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example ! Put additional information not in comments but edit your question: https://stackoverflow.com/posts/53248326/edit – jogo Nov 11 '18 at 11:55
  • I don't understand your expected output. what do `A1`, `B2`...mean? – Darren Tsai Nov 11 '18 at 11:55
  • @DarrenTsai : It gives the number of occurence of first charcter of each name . i.e. 2 people have name starting with A , 2 people have name starting with B and so on – S_Gupta Nov 11 '18 at 11:58

1 Answers1

-1
df <- data.frame(name = c("Alex", "Brad", "Brad"))
first_characters <- substr(df$name, 1, 1)
result <- sort(table(first_characters), decreasing = TRUE)
# from wide to long 
data.frame(result)
  • How to sort this in Descending and make the data come in Vertical Table rather than horizontal table – S_Gupta Nov 11 '18 at 12:06