Dplyr common functions for data manipulation (4)

Source: Internet
Author: User
Tags recode

Next we continue to understand some of the common functions in Dplyr.

1, ranking

Each of the following functions can be used to sort the data differently

Row_number (x)

Ntile (x, N)

Min_rank (x)

Dense_rank (x)

Percent_rank (x)

Cume_dist (x)

See some examples in detail.

X <-C (5, 1, 3, 2, 2, NA) x

Row_number (x)

Row_number is the size of the data to sort, encountered duplicate values, sorting continues to add 1, missing values do not count in

Min_rank (x)

Min_rank is to sort the data size, encounter duplicate values, and sort the same, but each value occupies one position and the missing value does not count toward

Dense_rank (x)

Dense_rank is to sort the data size, encounter duplicate values, sort the same, all duplicate values only one position, missing values do not count toward

Percent_rank (x)

Percent_rank is a percent sort of data, each representing a percentage position in the overall position of the data

Cume_dist (x)

Cume_dist is a numeric value that centers all the points in the 0-1, each of which represents the location of the data, a bit like percent_rank.

Ntile (x, 2)

Ntile can specify the number of data to sort, and then loop the sort data to sort the data.

  

2, Recode ()

Recode (. x, ...,. default = null,. Missing = null)

Data substitution functions, which are very flexible to use in data substitution.

x <-sample (C ("A", "B", "C"), and replace = TRUE) x

Recode (x, a = "Wo")

Recode (x, a = "Wo",. Default = Na_character_)

X<-sample (1:5,10,rep=t) x

Recode (x, ' 2 ' = 20L, ' 4 ' = 40L)

Recode (x, ' 2 ' = "B", ' 4 ' = "D")

Recode (x, "a", "B")

Recode (x, "a", "B",. Default = "Wo")

x <-C (1:4, NA) x

Recode_factor (x, ' 1 ' = "Z", ' 2 ' = "Y", ' 3 ' = "x")

Recode_factor (x, ' 1 ' = "Z", ' 2 ' = "Y",. Default = "D",. Missing = "M")

Recode_factor (Factor (Letters[1:3]), B = "Z", c = "Y")

Dplyr common functions for data manipulation (4)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.