0

I'm looking for a way to duplicates rows under certain conditions and by deleting some columns on R, using dplyr or another package.

I have a table like this one :

Ring  Date_d7  Date_d13  Info1  Info2
A     10/05    15/05     2      8
B     12/05    17/05     3      4

And I would like to obtain this one :

Ring Stage Date  Info1 Info2
A    d7    10/05 2     8 
A    d13   15/05 2     8 
B    d7    12/05 3     4 
B    d13   17/05 3     4 

I am sorry I don't understand how to make a table so it's not very nice to read... Also there might be already an answer but I coulnd't find it, I looked for a way to arrange my table with dplyr but with no success so far. Thank you by advance for your help !

tmfmnk
  • 38,881
  • 4
  • 47
  • 67
Coline
  • 1

1 Answers1

0

Try this ... kinda hacky I know, but it works.

# Create dummmy dataframe
Ring <- c('A','B'); Date_d7 <- c('10/05','12/05'); Date_d13 <- c('15/05','17/05'); Info1 <- c(2,3); Info2 <- c(8,4); df <- data.frame(Ring,Date_d7,Date_d13, Info1, Info2)

# Add future headers
df$stage7 <- 'd7'; df$stage13 <- 'd13'

# Split table
df1 <- subset(df, select = c(Ring, stage7, Date_d7, Info1, Info2)); colnames(df1)[2] <- 'Stage'; colnames(df1)[3] <- 'Date';
df2 <- subset(df, select = c(Ring, stage13, Date_d13, Info1, Info2)); colnames(df2)[2] <- 'Stage'; colnames(df2)[3] <- 'Date';

# Append for final dataframe
df_final <- rbind(df1, df2)
Monk
  • 407
  • 3
  • 8