Learn R Programming

m61r (version 0.0.3)

reshape: Reshape a data.frame

Description

Reshape a data.frame.

Usage

gather_(df, new_col_name = "parameters", new_col_values = "values",
  pivot)

spread_(df, col_name, col_values, pivot)

Arguments

df

data.frame

new_col_name

name of the new column 'parameters'

new_col_values

name of the new columns 'values'

col_name

name of the column 'parameters'

col_values

name of the new columns 'values'

pivot

name of the columns used as pivot

Value

The functions return a data frame.

  • Output from function gather_() get 'pivot' columns determined by argument pivot, and 'long' columns named according to arguments new_col_name and new_col_values.

  • Output from function spread_() get 'pivot' columns determined by argument pivot, and 'wide' columns named according to values in column determined by argument col_name. For 'wide' columns, each row corresponds to values present in column determined by argument col_values.

Details

A data frame is said 'wide' if several of its columns describe connected information of the same record. A data frame is said <U+2018>long<U+2019> if two of its columns provide information about records, with one describing their name and the second their value. Functions gather_() and spread_() enable to reshape a data frames from a <U+2018>wide<U+2019> format to a 'long' format, and vice-versa.

Examples

Run this code
# NOT RUN {
df3 <- data.frame(id = 1:4,
                    age = c(40,50,60,50),
                    dose.a1 = c(1,2,1,2),
                    dose.a2 = c(2,1,2,1),
                    dose.a14 = c(3,3,3,3))

gather_(df3,pivot = c("id","age"))

df4 <- gather_(df3,pivot = c("id","age"))
df5 <- rbind(df4,
    data.frame(id=5, age=20,parameters="dose.a14",values=8),
    data.frame(id=6, age=10,parameters="dose.a1",values=5))

spread_(df5,col_name="parameters",col_values="values",pivot=c("id","age"))

# }

Run the code above in your browser using DataLab