lundi 8 mai 2017

from long data table to wide data table

I am trying to transform the dataset. What i want to achieve is that if Intensitat = 1 - R creates a new column - Intensity 1a - and writes the value of damage_a. If Intensitat = 2 - R creates another new column - Intensity 2a - and writes the values of damage_a. Finally if Intensitat = 3 then R does new column - Intensity 3a - and writes the values of damage_a. (it always writes the value of damage_x corresponding to the same row as the intensity indicator).

Then I will run the same process: create 3 columns from an indicator variables - each column for an indicator value and in each column use the values of damage_b.

Lastly the same again but use the values of damage_c.

type <- sample(seq(from = 1, to = 5, by = 1), size = 50, replace = TRUE)
type

intensity <- sample(seq(from = 1, to = 3, by = 1), size = 50, replace = TRUE)
intensity

damage_a <- sample(seq(from = 1, to = 50, by = 1), size = 50, replace = TRUE)
damage_b <- sample(seq(from = 1, to = 200, by = 1), size = 50, replace = TRUE)
damage_c <- sample(seq(from = 1, to = 500, by = 1), size = 50, replace = TRUE)

dat <- data.frame(type, intensity, damage1, damage2, damage3)

# want to  evaluate if the difference in mean  between different damages 
  dat1 <- subset(dat, select = c(type, intensity, damage_a))
  dat2 <- subset(dat, select = c(type, intensity, damage_b))
  dat3 <- subset(dat, select = c(type, intensity, damage_c))

It might seem like an very easy task for you, but I have been stuck on this for two days. I have tried applyin tidyr function, ifelse comands, etc but never obtained what i needed. Any comments/ideas are very appriciated. Thank you.

Aucun commentaire:

Enregistrer un commentaire