一、初步认识tidyr包
1.首先安装tidyr包
install.package("tidyr")
2.初步了解tidyr包的知识
必应搜索tidyr,找到相关介绍tidyr包的知识(https://tidyr.tidyverse.org/, https://www.r-bloggers.com/data-manipulation-with-tidyr/)
tidyr包中主要涉及:
1)缺失值的简单补齐
2)长形表变宽形表与宽形表变长形表
3)列分割与列合并
二、初步应用tidyr包
1.缺失值的简单补齐
> library(tidyr)#加载包
> library(dplyr)#加载包
载入程辑包:‘dplyr’
The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
> #创建含有缺失值的数据框示例
> x <- c(1,2,7,8,NA,10,22,NA,15)
> y <-c('a',NA,'b',NA,'b','a','a','b','a')
> df <- data.frame(x = x, y = y)
> df
x y
1 1 a
2 2 <NA>
3 7 b
4 8 <NA>
5 NA b
6 10 a
7 22 a
8 NA b
9 15 a
> drop_na(df)
x y
1 1 a
3 7 b
6 10 a
7 22 a
9 15 a
> x <- c(1,2,7,8,NA,10,22,NA,15)
> y <-c('a',NA,'b',NA,'b','a','a','b','a')
> df <- data.frame(x = x, y = y)
> df
x y
1 1 a
2 2 <NA>
3 7 b
4 8 <NA>
5 NA b
6 10 a
7 22 a
8 NA b
9 15 a
> fill(df)#根据上一行的数值填充
x y
1 1 a
2 2 <NA>
3 7 b
4 8 <NA>
5 NA b
6 10 a
7 22 a
8 NA b
9 15 a
> fill(df,c(x,y))#根据上一行的数值填充
x y
1 1 a
2 2 a
3 7 b
4 8 b
5 8 b
6 10 a
7 22 a
8 22 b
9 15 a
> replace_na(df,list(x=2,y=b))#空值填进去特定的一个数值
Error in is_list(replace) : object 'b' not found
> replace_na(df,list(c(x=2,y=b))#空值填进去特定的一个数值
+ )
Error: unexpected input in:
"replace_na(df,list(c(x=2,y=b))#空值填进去特定的一个数值
?
> replace_na(df,list(c(x=2,y=b)))#空值填进去特定的一个数值
Error in is_list(replace) : object 'b' not found
> replace_na(df,list(x=2)#空值填进去特定的一个数值
+ )
x y
1 1 a
2 2 <NA>
3 7 b
4 8 <NA>
5 2 b
6 10 a
7 22 a
8 2 b
9 15 a
> replace_na(df,list(y=b))#空值填进去特定的一个数值
Error in is_list(replace) : object 'b' not found
> replace_na(df,list(y="b"))#空值填进去特定的一个数值
x y
1 1 a
2 2 b
3 7 b
4 8 b
5 NA b
6 10 a
7 22 a
8 NA b
9 15 a
> x <- c(1,2,7,8,NA,10,22,NA,15)
> y <-c('a',NA,'b',NA,'b','a','a','b','a')
> df <- data.frame(x = x, y = y)
> replace_na(df,list(x=2,y="b"))#空值填进去特定的一个数值
x y
1 1 a
2 2 b
3 7 b
4 8 b
5 2 b
6 10 a
7 22 a
8 2 b
9 15 a
> x <- c(1,2,7,8,NA,10,22,NA,15)
> y <-c('a',NA,'b',NA,'b','a','a','b','a')
> df <- data.frame(x = x, y = y)
> df
x y
1 1 a
2 2 <NA>
3 7 b
4 8 <NA>
5 NA b
6 10 a
7 22 a
8 NA b
9 15 a
> #计算x的均值和中位数
> x_mean <- mean(df$x, na.rm = TRUE)
> x_mean
[1] 9.285714
> #计算y的众数
> y_mode <- as.character(df$y[which.max(table(df$y))])
> y_mode
[1] "a"
> table(df$y)
a b
4 3
> which.max(table(df$y))
a
1
> f$y[which.max(table(df$y))]
Error: object 'f' not found
> df$y[which.max(table(df$y))]
[1] a
Levels: a b
> #替换数据框df中x和y的缺失值
> df2 <- replace_na(data = df, replace = list(x = x_mean, y = y_mode))
> df2
x y
1 1.000000 a
2 2.000000 a
3 7.000000 b
4 8.000000 a
5 9.285714 b
6 10.000000 a
7 22.000000 a
8 9.285714 b
9 15.000000 a
2.长形表变宽形表与宽形表变长形表
> #长形表
> name <- c('A','A','A','B','B')
> product <- c('P1','P2','P3','P1','P4')
> price <- c(100,130,55,100,78)
> df_long <- data.frame(name = name, product = product, price = price)
> df_long
name product price
1 A P1 100
2 A P2 130
3 A P3 55
4 B P1 100
5 B P4 78
> #宽形表
> name <- c('A','B','C')
> gender <- c('f','f','m')
> province <- c('JS','SH','HN')
> age <- c(18,22,19)
> df_wide <- data.frame(name = name, gender = gender, province = province, age = age)
> df_wide
name gender province age
1 A f JS 18
2 B f SH 22
3 C m HN 19
> #使用spread()函数实现长表转宽表,语法如下
> #spread(data, key, value, fill = NA, convert = FALSE, drop = TRUE)
> #data:为需要转换的长形表
> #key:需要将变量值拓展为字段的变量
> #value:需要分散的值
> #fill:对于缺失值,可将fill的值赋值给被转型后的缺失值
> df_long_expand <- spread(data = df_long, key = product, value = price)
> df_long_expand
name P1 P2 P3 P4
1 A 100 130 55 NA
2 B 100 NA NA 78
> #使用gather()函数实现宽表转长表,语法如下:
> #gather(data, key, value, ..., na.rm = FALSE, convert = FALSE)
> #data:需要被转换的宽形表
> #key:将原数据框中的所有列赋给一个新变量key
> #value:将原数据框中的所有值赋给一个新变量value
> #...:可以指定哪些列聚到一列中
> #na.rm:是否删除缺失值
> df_wide_gather <- gather(data = df_wide, key = variable, value = value)
Warning message:
attributes are not identical across measure variables;
they will be dropped
> df_wide_gather
variable value
1 name A
2 name B
3 name C
4 gender f
5 gender f
6 gender m
7 province JS
8 province SH
9 province HN
10 age 18
11 age 22
12 age 19
3.列分割与列合并
> id <- c(1,2)
> datetime <- c(as.POSIXlt('2015-12-31 13:23:44'), as.POSIXlt('2016-01-28 21:14:12'))
> df <- data.frame(id = id, datetime = datetime)
> df
id datetime
1 1 2015-12-31 13:23:44
2 2 2016-01-28 21:14:12
> #将日期时间数据切割为日期和时间两列
> separate <- df %>% separate(., col = datetime, into = c('date', 'time'), sep = ' ', remove = FALSE)
> separate
id datetime date time
1 1 2015-12-31 13:23:44 2015-12-31 13:23:44
2 2 2016-01-28 21:14:12 2016-01-28 21:14:12
网友评论