title: Getting the first letter from each row of a certain column in a data.frame in R
body: <p>I experience the following problem. One is given a data frame with 5 categories-a,b,c,d,e for each name(names are 54).
  I give you a small extract from the whole data frame in R just to give you a feeling on the topic.</p>
  <pre><code>                        **a       b      c           d      e
  Teniers                  15      12     13          6      G
  Van Dyck                 15      10     17         13      G
  Bourdon                  10       8      8          4      H
  Le Brun                  16      16      8         16      H
  Le Suer                  15      15      4         15      H
  Poussin                  15      17      6         15      H**
  </code></pre>
  <p>I have succeeded to arrange the names alphabetically with the &quot;sort&quot; function, so that not only the names column gets arranged alphabetically but their 5 categories belonging to each names moved as well. So far, so good, but the task is to take the first letter from each name and to select those names only whose beginning letters apear most often. I can get the first letters with the &quot;strsplit&quot; function, then the first letters appear on each row, but to the left ot them stays everywhere [1]&quot;the fist letter&quot;, new row[1] &quot;another first letter&quot;, new row1[...] till the 54th; and not the position in the dataframe..</p>
  <p>So, any ideas?</p>
  <p>Here is an extract from the code...</p>
  <pre><code>library(MASS)
  data(painters)
  attach(painters)
        painters
        str(painters)
        summary(painters)
  
  y &lt;- as.vector(rownames(painters))
       is.vector(y)
  
  
    
     
  sortnames &lt;- painters[order(y) , ]
  as.data.frame(  painters[order(y) , ] )   ##sorted in list; each name with ist relevant criteria
  
  rownames(sortnames)
  z &lt;- rownames(sortnames)
  str(z)
  is.vector(z)
  strsplit(z, &quot;&quot;)
  
  as.list(strsplit(z, &quot;&quot;))
  
  
  liste &lt;- as.list(strsplit(z, &quot;&quot;))
     matrix &lt;- as.matrix(liste)
     matrix
     matrix[,1]
     matrix[1,]
     matrix[1,1]
     matrix[[1]] [1]  
     
     first &lt;- matrix (as.matrix(liste))
     for(i in 1:54)  {print( matrix[[i]][1])  }    
     
     str(first) 
  </code></pre>
  <p>Regards and thanks for the fast response in advance!!</p>
  <h3>EDIT</h3>
  <p>what I need is:</p>
  <p>to create a vector(or a matrix with dimension[54,1]) that contains only the first letter of each name in the &quot;rownames&quot; column, each row of it should be the number of the row from the sorted vector in the data frame, so that we keep the position in the dataframe shown.</p>
  <p>e.g.</p>
  <pre><code>[1]&quot;A&quot;
  [2]&quot;B&quot;
  [3]&quot;B&quot;
  [4]&quot;C&quot;
  ....
  </code></pre>
  <p>In other words, one has to extract a vector/matrix with only the first letter of rownames(in the dataframe &quot;rownames&quot; is defined as only the painters names, so the very 1st column of the 6 ;) )</p>
  <p>I appreciate your help.</p>
  <pre><code>substr(data, 1, 1)
  </code></pre>
  <p>i got them like that:</p>
  <pre><code> firstletter &lt;- substr(rownames(sortnames), 1, 1)
   firstletter &lt;- as.data.frame(firstletter) **##how should I define &quot;firstletter&quot; for later use??**
   firstletter
   
  
  1            A
  2            B
  3            B
  4            B
  5            B
  6            C
  7            C
  8            C
  9            D
  10           D
  11           D
  12           D
  13           D
  14           D
  15           D
  16           F
  17           F
  18           F
  19           G
  20           G
  21           G
  22           H
  23           J
  24           J
  25           L
  26           L
  27           L
  28           L
  29           M
  30           M
  31           O
  32           P
  33           P
  34           P
  35           P
  36           P
  37           P
  38           P
  39           P
  40           P
  41           R
  42           R
  43           R
  44           T
  45           T
  46           T
  47           T
  48           T
  49           T
  50           V
  51           V
  52           V
  53           V
  54           V
  </code></pre>
  <p>worked like a charm. the first letter of the painters names is extracted and the row number stays as it should.</p>
  <p>So, thanks a lot !</p>
  <p>p.s. I have a last question only, is there a function or a command in R that can now take this &quot;firstletter&quot; [vector/matrix/list/data.frame] depends how we define its structure(what is the best decision? here for later use) and check which are the 3 most often appearing first letters in the vector/matrix/list and extracting only them? or it would be too complicated?</p>
  <p>EDIT: All i need is now just to delete the redundant last row from a certain matrix after a substract(rbind command)</p>
  <pre><code>                  firstletter Composition Drawing Colour Expression School
  Da Udine      &quot;D&quot;         &quot;10&quot;        &quot; 8&quot;    &quot;16&quot;   &quot; 3&quot;       &quot;A&quot;   
  Del Piombo    &quot;D&quot;         &quot; 8&quot;        &quot;13&quot;    &quot;16&quot;   &quot; 7&quot;       &quot;A&quot;   
  Diepenbeck    &quot;D&quot;         &quot;11&quot;        &quot;10&quot;    &quot;14&quot;   &quot; 6&quot;       &quot;G&quot;   
  Palma Giovane &quot;P&quot;         &quot;12&quot;        &quot; 9&quot;    &quot;14&quot;   &quot; 6&quot;       &quot;D&quot;   
  Palma Vecchio &quot;P&quot;         &quot; 5&quot;        &quot; 6&quot;    &quot;16&quot;   &quot; 0&quot;       &quot;D&quot;   
  Pordenone     &quot;P&quot;         &quot; 8&quot;        &quot;14&quot;    &quot;17&quot;   &quot; 5&quot;       &quot;D&quot;   
  Teniers       &quot;T&quot;         &quot;15&quot;        &quot;12&quot;    &quot;13&quot;   &quot; 6&quot;       &quot;G&quot;   
  The Carraci   &quot;T&quot;         &quot;15&quot;        &quot;17&quot;    &quot;13&quot;   &quot;13&quot;       &quot;E&quot;   
  Tintoretto    &quot;T&quot;         &quot;15&quot;        &quot;14&quot;    &quot;16&quot;   &quot; 4&quot;       &quot;D&quot;   
  Titian        &quot;T&quot;         &quot;12&quot;        &quot;15&quot;    &quot;18&quot;   &quot; 6&quot;       &quot;D&quot;   
  Da Vinci      &quot;D&quot;         &quot;15&quot;        &quot;16&quot;    &quot; 4&quot;   &quot;14&quot;       &quot;A&quot;   
  Domenichino   &quot;D&quot;         &quot;15&quot;        &quot;17&quot;    &quot; 9&quot;   &quot;17&quot;       &quot;E&quot;   
  Poussin       &quot;P&quot;         &quot;15&quot;        &quot;17&quot;    &quot; 6&quot;   &quot;15&quot;       &quot;H&quot;   
  The Carraci1  &quot;T&quot;         &quot;15&quot;        &quot;17&quot;    &quot;13&quot;   &quot;13&quot;       &quot;E&quot;   
  </code></pre>
  <p>Have googled for a long time and no function worked for me till now..</p>
  <p>Any suggestions?</p>