# Prepare data
dat <- data.frame(CAT = rownames(USPersonalExpenditure),
USPersonalExpenditure, stringsAsFactors = FALSE,
row.names = NULL)[1:4, ]
# View data
dat
# CAT X1940 X1945 X1950 X1955 X1960
# 1 Food and Tobacco 22.20 44.50 59.60 73.2 86.8
# 2 Household Operation 10.50 15.50 29.00 36.5 46.2
# 3 Medical and Health 3.53 5.76 9.71 14.0 21.1
# 4 Personal Care 1.04 1.98 2.45 3.4 5.4
# Default transpose
tdat1 <- proc_transpose(dat)
# View results
tdat1
# NAME COL1 COL2 COL3 COL4
# 1 X1940 22.2 10.5 3.53 1.04
# 2 X1945 44.5 15.5 5.76 1.98
# 3 X1950 59.6 29.0 9.71 2.45
# 4 X1955 73.2 36.5 14.00 3.40
# 5 X1960 86.8 46.2 21.10 5.40
# Transpose with ID and Name
tdat2 <- proc_transpose(dat, id = CAT, name = Year)
# View results
tdat2
# Year Food and Tobacco Household Operation Medical and Health Personal Care
# 1 X1940 22.2 10.5 3.53 1.04
# 2 X1945 44.5 15.5 5.76 1.98
# 3 X1950 59.6 29.0 9.71 2.45
# 4 X1955 73.2 36.5 14.00 3.40
# 5 X1960 86.8 46.2 21.10 5.40
# Transpose only some of the variables
tdat3 <- proc_transpose(dat, var = v(X1940, X1950, X1960), id = CAT, name = Year)
# View results
tdat3
# Year Food and Tobacco Household Operation Medical and Health Personal Care
# 1 X1940 22.2 10.5 3.53 1.04
# 2 X1950 59.6 29.0 9.71 2.45
# 3 X1960 86.8 46.2 21.10 5.40
# By with a where clause
tdat4 <- proc_transpose(dat, by = CAT, name = Year,
where = expression(Year %in% c("X1940", "X1950", "X1960")))
# View Results
tdat4
# CAT Year COL1
# 1 Food and Tobacco X1940 22.20
# 2 Food and Tobacco X1950 59.60
# 3 Food and Tobacco X1960 86.80
# 4 Household Operation X1940 10.50
# 5 Household Operation X1950 29.00
# 6 Household Operation X1960 46.20
# 7 Medical and Health X1940 3.53
# 8 Medical and Health X1950 9.71
# 9 Medical and Health X1960 21.10
# 10 Personal Care X1940 1.04
# 11 Personal Care X1950 2.45
# 12 Personal Care X1960 5.40
Run the code above in your browser using DataLab