dataframe - Easy way to transform data frame in R - one variable values as separate columns -


this question has answer here:

what have (obviously i'm presenting small fraction of current data):

my_df <- structure(list(x = structure(c(48.75, 49.25), .dim = 2l), y = structure(c(17.25, 17.25), .dim = 2l), time = structure(c(14625, 14626), .dim = 2l, class = "date"), spei = c(-0.460236400365829, -0.625695407390594)), .names = c("x", "y", "time", "spei"), row.names = 1:2, class = "data.frame") 

what need:

new_df <- structure(list(x = structure(c(48.75, 49.25), .dim = 2l), y = structure(c(17.25, 17.25), .dim = 2l), "2010-01-16" = c(-0.460236400365829, nan), "2010-01-17" = c(nan, -0.625695407390594)), .names = c("x", "y", "2010-01-16", "2010-01-17"), row.names = 1:2, class = "data.frame") 

what easiest way of doing this? thought writing loop, guess apply/sapply might on this?

transformation needed

you can use library tidyr , spread function this:

library(tidyr) spread(my_df, time, spei)       x     y 2010-01-16 2010-01-17 1 48.75 17.25 -0.4602364         na 2 49.25 17.25         na -0.6256954 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -