How to change all the factor levels into lowercase and add the amount of the levels

Hi guys, I was wondering how to turn all the factor levels (they are letters eg. 'a', 'A', 'b', 'B') into lowercase and add the amount of these levels, amount meaning like:

# location.first.char =
> location.first.char
   [1] U    U    U    U    U    U    U    U    U    U    U    U    U    U    U    U    U   
  [18] U    U    U    U    U    U    U    U    U    U    U    U    U    U    B    E    A   
  [35] P    L    P    W    U    P    L    U    m    U    U    U    G    U    L    E    S   
  [52] I    B    A    A    U    S    S    h    E    U    I    T    C    C    B    S    C   
  [69] N    S    U    S    W    A    N    U    N    S    N    N    U    Y    U    A    S   
  [86] U    N    M    M    W    U    N    U    g    A    C    B    H    G    U    U    N   
 [103] I    <NA> S    <NA> U    <NA> I    <NA> U    S    U    B    S    U    U    d    S   
 [120] S    O    B    U    U    U    <NA> <NA> m    <NA> H    U    U    U    <NA> <NA> U   
 [137] <NA> P    S    P    U    S    J    H    E    b    F    U    I    y    N    S    U   
 [154] M    s    N    U    L    C    U    T    H    I    <NA> U    I    F    S    i    n   
 [171] T    U    J    N    U    N    W    U    H    C    A    M    U    A    S    U    H   
 [188] U    W    N    U    K    H    H    L    <NA> U    L    U    C    V    L    U    U   
 [205] H    U    U    P    D    H    H    N    B    U    U    U    V    G    B    U    U   
 [222] T    U    U    U    U    t    L    T    U    S    U    W    P    U    <NA> U    U   
 [239] U    S    U    L    A    C    U    U    U    W    S    S    U    C    Y    U    C   
 [256] U    U    U    R    C    R    U    U    P    L    C    T    U    U    S    B    C   
 [273] C    s    U    U    T    E    T    U    T    A    O    U    U    A    U    B    U   
 [290] U    S    S    O    H    B    U    U    U    L    U    U    U    T    U    C    C   
 [307] <NA> U    U    A    U    W    U    p    C    E    U    C    U    C    N    H    R   
 [324] D    N    F    W    U    N    E    C    T    U    T    C    H    E    U    <NA> m   
 [341] K    U    U    N    <NA> P    U    U    U    U    U    U    t    c    M    U    S   
 [358] H    U    N    U    U    P    M    U    U    C    U    H    N    U    U    U    U   
 [375] W    B    N    U    U    <NA> U    S    U    C    O    U    U    J    K    M    Z   
 [392] R    U    b    U    E    <NA> J    M    U    L    N    E    U    C    E    J    U   
 [409] M    U    U    U    s    A    E    s    W    B    h    s    t    <NA> A    N    S   
 [426] O    s    W    s    U    A    s    W    U    C    s    s    N    U    s    s    s   
 [443] U    U    U    U    D    N    U    L    S    U    U    I    F    U    D    T    I   
 [460] S    U    B    W    <NA> U    S    U    O    U    r    H    M    p    F    U    L   
 [477] <NA> U    D    U    G    p    P    C    L    T    <NA> C    a    M    A    U    U   
 [494] U    U    L    l    U    P    U    L    L    S    <NA> G    L    A    U    A    A   
 [511] U    i    T    <NA> S    i    O    L    L    g    U    S    N    I    U    U    L   
 [528] S    U    N    S    U    J    U    U    D    B    B    <NA> B    B    P    F    C   
 [545] W    U    D    N    U    U    U    N    J    U    U    S    U    S    U    D    B   
 [562] U    E    U    U    U    W    W    C    N    U    A    C    W    N    U    W    U   
 [579] B    S    U    P    A    M    W    U    M    i    U    U    O    U    U    A    U   
 [596] P    N    A    J    A    A    M    W    U    O    E    U    E    U    W    U    O   
 [613] S    U    U    U    S    U    U    N    U    N    H    P    U    U    t    U    U   
 [630] U    U    B    U    U    U    U    U    U    S    S    C    U    U    U    E    C   
 [647] U    T    A    F    U    U    U    A    R    d    U    R    U    <NA> C    P    U   
 [664] M    T    D    D    A    P    K    s    U    M    U    S    G    U    M    D    U   
 [681] P    I    U    N    U    U    U    T    U    B    S    U    Y    L    U    U    w   
 [698] U    U    S    U    U    S    E    K    S    S    N    N    E    U    P    U    R   
 [715] U    A    U    U    V    I    U    I    B    F    d    N    U    Q    U    U    U   
 [732] U    I    L    U    G    U    M    B    A    U    A    <NA> M    T    U    A    U   
 [749] A    U    G    I    t    U    U    U    S    <NA> <NA> W    I    v    U    U    U   
 [766] F    U    c    N    U    U    H    B    Q    V    U    U    O    w    P    C    K   
 [783] S    U    L    M    D    I    U    C    B    U    U    P    C    U    J    M    U   
 [800] U    L    U    S    L    V    C    K    K    U    D    U    U    U    U    G    W   
 [817] c    C    W    S    U    H    U    C    U    O    U    U    U    C    U    U    H   
 [834] l    T    W    T    U    U    U    O    U    U    U    U    U    U    I    U    U   
 [851] U    C    U    U    <NA> U    D    C    U    U    U    U    S    U    B    U    c   
 [868] U    I    U    T    H    U    U    P    <NA> I    N    E    B    U    B    I    B   
 [885] W    U    M    I    S    D    E    U    C    U    N    L    <NA> W    L    L    S   
 [902] A    U    L    G    U    S    S    C    p    U    U    P    S    u    I    B    A   
 [919] S    I    L    O    <NA> U    f    U    T    G    J    T    U    S    C    <NA> S   
 [936] C    G    N    k    L    M    U    U    U    A    L    L    G    U    G    U    M   
 [953] N    N    U    s    U    N    N    N    M    U    U    U    C    G    N    N    U   
 [970] P    U    C    N    U    P    U    U    P    U    N    N    P    U    N    U    h   
 [987] S    A    <NA> U    <NA> <NA> N    T    N    U    P    C    U    F   
 [ reached getOption("max.print") -- omitted 9876 entries ]
52 Levels: a A b B c C d D e E f F g G h H i I j J k K l L m M n N o O p P q Q r R s ... Z

# This is  the 'amount' I am referring to:
> table(location.first.char)
location.first.char
   a    A    b    B    c    C    d    D    e    E    f    F    g    G    h    H    i    I 
  31  334   38  322   42  492   22  208   18  190   15  137   11  139   30  217   48  224 
   j    J    k    K    l    L    m    M    n    N    o    O    p    P    q    Q    r    R 
   7   78   13  144   42  408   38  415   27  558   16  141   36  313    3   25   13  136 
   s    S    t    T    u    U    v    V    w    W    x    X    y    Y    z    Z 
  77  590   55  299   12 4029    8   90   45  326    1    2    7   35    1    6 

When the uppercase levels become lowercase and therefore become the same level as they would now both be eg. a. (both would be lowercase) Okay take 'b' and 'B', when the uppercase version becomes lowercase I want it so there wouldn't be two lowercase 'b's, I also want to add the amount of said 'b' and 'B' which in the case above would equal 360

I assume this is homework, given the similarity to another question, and I refer you to your classmate's query from a few hours ago here.

1 Like

Orchid06 asking to add a variable I want the amount, amount meaning if say 'a' was present 10 times in the variable column and uppercase 'A' was present 5 times, I would want the new transformed variable to be the singular lowercase 'a' and it's amount would then equal 15 times (10 + 5).

Ah, right. I think it's basically the same idea as for @Orchid06's query except change summarise(sum(letters_02)) to summarise(n=n()).

In a non-tidyverse way it might be something like (untested) table(tolower(location.first.char))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.