Home > front end >  Data manipulation for plotting in r, getting NULLs
Data manipulation for plotting in r, getting NULLs

Time:10-23

I once used the code below to sort my data for a dumbbell plot, I tried reusing the code for different data but I am getting an empty output


data10 <- structure(list(Trial_type = c(1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 
1), Trial_type2 = c(1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1), GROUP = c("LLL", 
"LLL", "LLL", "LRL", "LRL", "LRL", "RLR", "RLR", "RLR", "RRR", 
"RRR", "RRR"), conditon2 = c("CEN_LLL", "IPS_LLL", "CTL_LLL", 
"CEN_LRL", "IPS_LRL", "CTL_LRL", "CEN_RLR", "IPS_RLR", "CTL_RLR", 
"CEN_RRR", "IPS_RRR", "CTL_RRR"), condition20 = c(1, 2, 3, 1, 
2, 3, 1, 2, 3, 1, 2, 3), Training = c("left", "left", "left", 
"right", "right", "right", "left", "left", "left", "right", "right", 
"right"), AveResultantVel_102 = c(2.150005313, 1.854148813, 1.647962313, 
2.35681725, 2.067673063, 1.10213475, 2.364870813, 2.027195438, 
1.61692725, 2.111901813, 2.026179, 1.595148125), AveResultantVel_104 = c(2.37879375, 
2.127869563, 1.903676063, 2.932732875, 2.230088313, 1.311275125, 
2.69564575, 2.473001938, 1.926669438, 2.54519575, 2.201091438, 
1.902556875)), row.names = c(NA, -12L), class = c("tbl_df", "tbl", 
"data.frame"))
library(tidyverse)

data10A <- data10 %>% select(conditon2,Trial_type,AveResultantVel_102,AveResultantVel_104) %>% mutate("key"="Change in resultant velocity (cm/s)")
  data10A$Trial_type <- factor(data10A$Trial_type, levels = 1:2, labels = c("Retention", "Transfer"))
  i1 <- grepl("_RR", levels(data10A$conditon2))
  i2 <- grepl("_RL", levels(data10$conditon2))
  i3 <- grepl("_LL", levels(data10A$conditon2))
  RRR_levels <- levels(data10A$conditon2)[i1]
  RLR_levels <- levels(data10A$conditon2)[i2]
  LLL_levels <- levels(data10A$conditon2)[i3]
  LRL_levels <- levels(data10A$conditon2)[!i1 & !i2 & !i3]
  ord_levels <- c(LLL_levels, RRR_levels, RLR_levels, LRL_levels)
  data10A$conditon2 <- factor(data10A$conditon2, levels = ord_levels)
  

CodePudding user response:

condition2 is of character type so it does not have levels. Change it to factor.

data10A$conditon2 <- factor(data10A$conditon2)

An alternative would be to use unique(data10A$conditon2) instead of levels(data10A$conditon2) which will work for both character and factor data.


Complete code -

library(dplyr)

data10A <- data10 %>% 
  select(conditon2,Trial_type,AveResultantVel_102,AveResultantVel_104) %>% 
  mutate("key"="Change in resultant velocity (cm/s)")
data10A$Trial_type <- factor(data10A$Trial_type, levels = 1:2, labels = c("Retention", "Transfer"))
data10A$conditon2 <- factor(data10A$conditon2)
i1 <- grepl("_RR", levels(data10A$conditon2))
i2 <- grepl("_RL", levels(data10A$conditon2))
i3 <- grepl("_LL", levels(data10A$conditon2))
RRR_levels <- levels(data10A$conditon2)[i1]
RLR_levels <- levels(data10A$conditon2)[i2]
LLL_levels <- levels(data10A$conditon2)[i3]
LRL_levels <- levels(data10A$conditon2)[!i1 & !i2 & !i3]
ord_levels <- c(LLL_levels, RRR_levels, RLR_levels, LRL_levels)
data10A$conditon2 <- factor(data10A$conditon2, levels = ord_levels)
  • Related