首先构造一个二维数据框
df<- data.frame(x=c("A","B","C"),"2010"=c(1,3,4),"2011"=c(3,5,2),check.names = FALSE)
#1.二维数据→一维数据
df_melt <- reshape2::melt(df,id.vars="x",variable.name="year",value.name="value")
df_gather <- tidyr::gather(df,year,value,-x)
#2.一维数据→二维数据
df_dcast <- reshape2::dcast(df_melt,x~year,value.var = "value")
df_spread <- tidyr::spread(df_gather,year,value)
#3.在原有数据框基础添加新列且赋值
df_transform <- transform(df_melt,value2=value*2)
df_transform2 <- transform(df_melt,value2=ifelse(year=="2011",value*3,value))
df_mutate <- dplyr::mutate(df_melt,value2=ifelse(year=="2011",value*3,value))
#4.排序
df_arrange1 <- dplyr::arrange(df_melt,value) 升序排序
df_arrange2 <- dplyr::arrange(df_melt,desc(value)) 降序排序