I have a dataset containing Sample's ID, Traits, & trait values:
df <- data.frame( ID = c(001, 001, 001, 001, 002, 002, 002,002), Traits = c('Height','Weight','N_content','C_content','Height','Weight','N_content','C_content'),Trait_val = c(0.5,1,0.2,0.2,0.6,2,0.2,0.4))
print(df)
ID Traits Trait_val
001 Height 0.5
001 Weight 1
001 N_content 0.2
001 C_content 0.2
002 Height 0.6
002 Weight 2
002 N_content 0.2
002 C_content 0.4
I would like to have a dataframe like this:
ID Height Weight N_content C_content
001 0.5 1 0.2 0.2
002 0.6 2 0.2 0.4
I've been thinking about filtering each trait with their corresponding IDs and trait values and then joining the dataframes to have all the data together, but I think there should be a better way to do this. Please let me know if there is a faster way to do this. Thanks!