Search code examples
ranalysis

Is there a methodology to assign integer values to factors in R


I am quite new to R, but was wondering if there is a specific way to group/analyze integer values from my data frame i.e.,

Sample X : int 1 2 3 4 5

Sample Y : int 6 7 8 9 10

Sample Z : int 11 12 13 14 15

and assign these to my factor variable which has the corresponding number of levels (5 in this example) which are called in this example lvl 1, lvl 2, lvl 3, lvl 4, lvl 5. The goal is to be able to graph the observations at each level, for example lvl 1 had the observations 1, 6, and 11/ lvl 2 had 2, 7, and 12, etc.

I've found no clean way to do this. Other attempts have including individually typing out the name of each sample and manually linking this to the factor levels, but that has not gone well.

Any advice would be appreciated!


Solution

  • If I understood correctly, you want to have each x, y and z observations associated with a level and plot by level.

    library(ggplot2)
    library(reshape2)
    
    df = data.frame(x = 1:5, y = 6:10, z = 11:15)
    
    df$level = factor(paste0("lvl",1:5))
    
    df
    df
    #  x  y  z level
    # 1 1  6 11  lvl1
    # 2 2  7 12  lvl2
    # 3 3  8 13  lvl3
    # 4 4  9 14  lvl4
    # 5 5 10 15  lvl5
    

    It's easier to use long formatted data for plot (with ggplot2 package). I use reshape2::melt here but you could find equivalent solution with tidyr::pivot_long

    df <- reshape2::melt(df, id.vars = "level")
    df
       level variable value
    1   lvl1        x     1
    2   lvl2        x     2
    3   lvl3        x     3
    4   lvl4        x     4
    5   lvl5        x     5
    6   lvl1        y     6
    7   lvl2        y     7
    8   lvl3        y     8
    9   lvl4        y     9
    10  lvl5        y    10
    11  lvl1        z    11
    12  lvl2        z    12
    13  lvl3        z    13
    14  lvl4        z    14
    15  lvl5        z    15
    

    Finally, you can plot. Let's say you want points for each level:

    ggplot(df, aes(x = level, y = value)) + geom_point()
    

    enter image description here