R语言数据处理——表格转换
程序员文章站
2022-07-14 21:15:11
...
1.定义表格:
df <- data.frame(x = c('A','B','C'),'2018' = c(1,3,4),'2019' = c(3,5,2),check.names = FALSE)
head(df)
控制台输出:
> df <- data.frame(x = c('A','B','C'),'2018' = c(1,3,4),'2019' = c(3,5,2),check.names = FALSE)
> head(df)
x 2018 2019
1 A 1 3
2 B 3 5
3 C 4 2
注意一点的是,如果写成脚本形式,最好一步一步的点“Run”。
2.(方法1)将宽数据转换为长数据,将多行聚集成列
df_melt <- reshape2::melt(df,id.vars = "x",variable.name = "year",value.name = "value")
head(df_melt)
控制台输出:
> df_melt <- reshape2::melt(df,id.vars = "x",variable.name = "year",value.name = "value")
> head(df_melt)
x year value
1 A 2018 1
2 B 2018 3
3 C 2018 4
4 A 2019 3
5 B 2019 5
6 C 2019 2
(方法2)
df_gather <- tidyr::gather(df,year,value,-x)
head(df_gather)
控制台输出:
> df_gather <- tidyr::gather(df,year,value,-x)
> head(df_gather)
x year value
1 A 2018 1
2 B 2018 3
3 C 2018 4
4 A 2019 3
5 B 2019 5
6 C 2019 2
3.(方法1)将长数据转换为宽数据
df_cast <- reshape2::dcast(df_melt, x~year,value.var = 'value')
head(df_cast)
控制台输出:
> df_cast <- reshape2::dcast(df_melt, x~year,value.var = 'value')
> head(df_cast)
x 2018 2019
1 A 1 3
2 B 3 5
3 C 4 2
(方法2)
df_spread <- tidyr::spread(df_gather,year,value)
head(df_spread)
4.变量的变换
方法1.
dat1 <- transform(df_melt,value2 = value*2)
head(dat1)
控制台输出:
> dat1 <- transform(df_melt,value2 = value*2)
> head(dat1)
x year value value2
1 A 2018 1 2
2 B 2018 3 6
3 C 2018 4 8
4 A 2019 3 6
5 B 2019 5 10
6 C 2019 2 4
方法2.
dat2 <- transform(df_melt,value2 = ifelse(year=="2018",value*2,value+1))
head(dat2)
控制台输出:
> dat2 <- transform(df_melt,value2 = ifelse(year=="2018",value*2,value+1))
> head(dat2)
x year value value2
1 A 2018 1 2
2 B 2018 3 6
3 C 2018 4 8
4 A 2019 3 4
5 B 2019 5 6
6 C 2019 2 3
方法3.
dat2 <- dplyr::mutate(df_melt,value2 = ifelse(year=="2018",value*2,value+1))
head(dat2)
5.表格的排序
升序:
dat_arrange1 <- dplyr::arrange(df_melt,value)
head(dat_arrange1)
控制台输出:
> dat_arrange1 <- dplyr::arrange(df_melt,value)
> head(dat_arrange1)
x year value
1 A 2018 1
2 C 2019 2
3 B 2018 3
4 A 2019 3
5 C 2018 4
6 B 2019 5
降序:
dat_arrange2 <- dplyr::arrange(df_melt,desc(value))
head(dat_arrange2)
控制台输出:
> dat_arrange2 <- dplyr::arrange(df_melt,desc(value))
> head(dat_arrange2)
x year value
1 B 2019 5
2 C 2018 4
3 B 2018 3
4 A 2019 3
5 C 2019 2
6 A 2018 1
全部的脚本:
# 表格的变换
df <- data.frame(x = c('A','B','C'),'2018' = c(1,3,4),'2019' = c(3,5,2),check.names = FALSE)
head(df)
df_melt <- reshape2::melt(df,id.vars = "x",variable.name = "year",value.name = "value")
head(df_melt)
df_gather <- tidyr::gather(df,year,value,-x)
head(df_gather)
df_cast <- reshape2::dcast(df_melt, x~year,value.var = 'value')
head(df_cast)
df_spread <- tidyr::spread(df_gather,year,value)
head(df_spread)
# 变量的变换
dat1 <- transform(df_melt,value2 = value*2)
head(dat1)
dat2 <- transform(df_melt,value2 = ifelse(year=="2018",value*2,value+1))
head(dat2)
dat2 <- dplyr::mutate(df_melt,value2 = ifelse(year=="2018",value*2,value+1))
head(dat2)
# 表格的排序
dat_arrange1 <- dplyr::arrange(df_melt,value)
head(dat_arrange1)
dat_arrange2 <- dplyr::arrange(df_melt,desc(value))
head(dat_arrange2)
操作:
上一篇: 思岚A1激光雷达hector_mapping建图与定位
下一篇: 软件构造 SOLID设计原则