[R] reshaping data

B77S bps0002 at auburn.edu
Wed Sep 7 17:24:01 CEST 2011


The terminology (melt, cast, recast) just isn't intuitive to me; but I
understand how to use melt now.
Thanks!  



Justin Haynes wrote:
> 
> look at the melt function in reshape, specifically ?melt.data.frame
> 
> require(reshape)
> Raw.melt<-melt(RawData,id.vars='Year',variable_name='Month')
> 
> there is an additional feature in the melt function for handling na
> values.
> names(Raw.melt)[3]<-'CO2'
> 
>> head(Raw.melt)
>   Year Month    CO2
> 1 1958     J     NA
> 2 1959     J 315.58
> 3 1960     J 316.43
> 4 1961     J 316.89
> 5 1962     J 317.94
> 6 1963     J 318.74
>>
> 
> you can order your data.frame if you'd like
> 
> Raw.melt<-Raw.melt[order(Raw.melt$Year,Raw.melt$Month),]
> 
>> head(Raw.melt)
>     Year Month    CO2
> 1   1958     J     NA
> 48  1958     F     NA
> 95  1958     M 315.71
> 142 1958     A 317.45
> 189 1958   M.1 317.50
> 236 1958   J.1     NA
> 
> 
> On Wed, Sep 7, 2011 at 7:35 AM, B77S <bps0002 at auburn.edu> wrote:
> 
>> I have the following data (see RawData using dput below)
>>
>> How do I get it in the following 3 column format (CO2 measurements are
>> the
>> elements of the original data frame).  I'm sure the package reshape is
>> where
>> I should look, but I haven't figured out how.
>>
>> Thanks ahead of time
>>
>>  Month Year CO2
>> J       1958
>> F       1958
>> M       1958    315.71
>> A       1958    317.45
>> M.1     1958    317.5
>> J.1     1958
>> J.2     1958    315.86
>> A.1     1958    314.93
>> S       1958    313.19
>> O       1958
>> N       1958    313.34
>> D       1958    314.67
>> J       1959    315.58
>> F       1959    316.47
>>
>>
>> # here is the data
>>
>> RawData <- structure(list(Year = c(1958, 1959, 1960, 1961, 1962, 1963,
>> 1964,
>> 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975,
>> 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986,
>> 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
>> 1998, 1999, 2000, 2001, 2002, 2003, 2004), J = c(NA, 315.58,
>> 316.43, 316.89, 317.94, 318.74, 319.57, 319.44, 320.62, 322.33,
>> 322.57, 324, 325.06, 326.17, 326.77, 328.54, 329.35, 330.4, 331.74,
>> 332.92, 334.97, 336.23, 338.01, 339.23, 340.75, 341.37, 343.7,
>> 344.97, 346.29, 348.02, 350.43, 352.76, 353.66, 354.72, 355.98,
>> 356.7, 358.36, 359.96, 362.05, 363.18, 365.32, 368.15, 369.14,
>> 370.28, 372.43, 374.68, 376.79), F = c(NA, 316.47, 316.97, 317.7,
>> 318.56, 319.08, NA, 320.44, 321.59, 322.5, 323.15, 324.42, 325.98,
>> 326.68, 327.63, 329.56, 330.71, 331.41, 332.56, 333.42, 335.39,
>> 336.76, 338.36, 340.47, 341.61, 342.52, 344.51, 346, 346.96,
>> 348.47, 351.72, 353.07, 354.7, 355.75, 356.72, 357.16, 358.91,
>> 361, 363.25, 364, 366.15, 368.87, 369.46, 371.5, 373.09, 375.63,
>> 377.37), M = c(315.71, 316.65, 317.58, 318.54, 319.69, 319.86,
>> NA, 320.89, 322.39, 323.04, 323.89, 325.64, 326.93, 327.18, 327.75,
>> 330.3, 331.48, 332.04, 333.5, 334.7, 336.64, 337.96, 340.08,
>> 341.38, 342.7, 343.1, 345.28, 347.43, 347.86, 349.42, 352.22,
>> 353.68, 355.39, 357.16, 357.81, 358.38, 359.97, 361.64, 364.03,
>> 364.57, 367.31, 369.59, 370.52, 372.12, 373.52, 376.11, 378.41
>> ), A = c(317.45, 317.71, 319.03, 319.48, 320.58, 321.39, NA,
>> 322.13, 323.7, 324.42, 325.02, 326.66, 328.13, 327.78, 329.72,
>> 331.5, 332.65, 333.31, 334.58, 336.07, 337.76, 338.89, 340.77,
>> 342.51, 343.56, 344.94, 347.08, 348.35, 349.55, 350.99, 353.59,
>> 355.42, 356.2, 358.6, 359.15, 359.46, 361.26, 363.45, 364.72,
>> 366.35, 368.61, 371.14, 371.66, 372.87, 374.86, 377.65, 380.52
>> ), M.1 = c(317.5, 318.29, 320.03, 320.58, 321.01, 322.24, 322.23,
>> 322.16, 324.07, 325, 325.57, 327.38, 328.07, 328.92, 330.07,
>> 332.48, 333.09, 333.96, 334.87, 336.74, 338.01, 339.47, 341.46,
>> 342.91, 344.13, 345.75, 347.43, 348.93, 350.21, 351.84, 354.22,
>> 355.67, 357.16, 359.34, 359.66, 360.28, 361.68, 363.79, 365.41,
>> 366.79, 369.29, 371, 371.82, 374.02, 375.55, 378.35, 380.63),
>>    J.1 = c(NA, 318.16, 319.59, 319.78, 320.61, 321.47, 321.89,
>>    321.87, 323.75, 324.09, 325.36, 326.7, 327.66, 328.57, 329.09,
>>    332.07, 332.25, 333.59, 334.34, 336.27, 337.89, 339.29, 341.17,
>>    342.25, 343.35, 345.32, 346.79, 348.25, 349.54, 351.25, 353.79,
>>    355.13, 356.22, 358.24, 359.25, 359.6, 360.95, 363.26, 364.97,
>>    365.62, 368.87, 370.35, 371.7, 373.3, 375.4, 378.13, 379.57
>>    ), J.2 = c(315.86, 316.55, 318.18, 318.58, 319.61, 319.74,
>>    320.44, 321.21, 322.4, 322.55, 324.14, 325.89, 326.35, 327.37,
>>    328.05, 330.87, 331.18, 331.91, 333.05, 334.93, 336.54, 337.73,
>>    339.56, 340.49, 342.06, 343.99, 345.4, 346.56, 347.94, 349.52,
>>    352.39, 353.9, 354.82, 356.17, 357.03, 357.57, 359.55, 361.9,
>>    363.65, 364.47, 367.64, 369.27, 370.12, 371.62, 374.02, 376.62,
>>    377.79), A.1 = c(314.93, 314.8, 315.91, 316.79, 317.4, 317.77,
>>    318.7, 318.87, 320.37, 320.92, 322.11, 323.67, 324.69, 325.43,
>>    326.32, 329.31, 329.4, 330.06, 330.94, 332.75, 334.68, 336.09,
>>    337.6, 338.43, 339.82, 342.39, 343.28, 344.69, 345.91, 348.1,
>>    350.44, 351.67, 352.91, 354.03, 355, 355.52, 357.49, 359.46,
>>    361.49, 362.51, 365.77, 366.94, 368.12, 369.55, 371.49, 374.5,
>>    375.86), S = c(313.19, 313.84, 314.16, 314.99, 316.26, 316.21,
>>    316.7, 317.81, 318.64, 319.26, 320.33, 322.38, 323.1, 323.36,
>>    324.84, 327.51, 327.44, 328.56, 329.3, 331.58, 332.76, 333.91,
>>    335.88, 336.69, 337.97, 339.86, 341.07, 343.09, 344.86, 346.44,
>>    348.72, 349.8, 350.96, 352.16, 353.01, 353.7, 355.84, 358.06,
>>    359.46, 360.19, 363.9, 364.63, 366.62, 367.96, 370.71, 372.99,
>>    374.06), O = c(NA, 313.34, 313.83, 315.31, 315.42, 315.99,
>>    316.87, 317.3, 318.1, 319.39, 320.25, 321.78, 323.07, 323.56,
>>    325.2, 327.18, 327.37, 328.34, 328.94, 331.16, 332.54, 333.86,
>>    336.01, 336.85, 337.86, 339.99, 341.35, 342.8, 344.17, 346.36,
>>    348.88, 349.99, 351.18, 352.21, 353.31, 353.98, 355.99, 357.75,
>>    359.6, 360.77, 364.23, 365.12, 366.73, 368.09, 370.24, 373,
>>    374.24), N = c(313.34, 314.81, 315, 316.1, 316.69, 317.07,
>>    317.68, 318.87, 319.79, 320.72, 321.32, 322.85, 324.01, 324.8,
>>    326.5, 328.16, 328.46, 329.49, 330.31, 332.4, 333.92, 335.29,
>>    337.1, 338.36, 339.26, 341.16, 342.98, 344.24, 345.66, 347.81,
>>    350.07, 351.3, 352.83, 353.75, 354.16, 355.33, 357.58, 359.56,
>>    360.76, 362.43, 365.46, 366.67, 368.29, 369.68, 372.08, 374.35,
>>    375.86), D = c(314.67, 315.59, 316.19, 317.01, 317.69, 318.36,
>>    318.71, 319.42, 321.03, 321.96, 322.9, 324.12, 325.13, 326.01,
>>    327.55, 328.64, 329.58, 330.76, 331.68, 333.85, 334.95, 336.73,
>>    338.21, 339.61, 340.49, 342.99, 344.22, 345.56, 346.9, 348.96,
>>    351.34, 352.53, 354.21, 354.99, 355.4, 356.8, 359.04, 360.7,
>>    362.33, 364.28, 366.97, 368.01, 369.53, 371.24, 373.78, 375.7,
>>    377.48)), .Names = c("Year", "J", "F", "M", "A", "M.1", "J.1",
>> "J.2", "A.1", "S", "O", "N", "D"), row.names = c(NA, -47L), class =
>> "data.frame")
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/reshaping-data-tp3796246p3796246.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

--
View this message in context: http://r.789695.n4.nabble.com/reshaping-data-tp3796246p3796399.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list