R – Sorting A Data Frame By The Contents Of A Column - R-bloggers
Maybe your like
Let’s examine how to sort the contents of a data frame by the value of a column
> numPeople = 10 > sex=sample(c("male","female"),numPeople,replace=T) > age = sample(14:102, numPeople, replace=T) > income = sample(20:150, numPeople, replace=T) > minor = age<18This last statement might look surprising if you’re used to Java or a traditional programming language. Rather than becoming a single boolean/truth value, minor actually becomes a vector of truth values, one per row in the age column. It’s equivalent to the much more verbose code in Java:
int[] age= ...; for (int i = 0; i < income.length; i++) { minor[i] = age[i] < 18; }Just as expected, the value of minor is a vector:
> mode(minor) [1] "logical" > minor [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSENext we create a data frame, which groups together our various vectors into the columns of a data structure:
> population = data.frame(sex=sex, age=age, income=income, minor=minor) > population sex age income minor 1 male 68 150 FALSE 2 male 48 21 FALSE 3 female 68 58 FALSE 4 female 27 124 FALSE 5 female 84 103 FALSE 6 male 92 112 FALSE 7 male 35 65 FALSE 8 female 15 117 TRUE 9 male 89 95 FALSE 10 male 26 54 FALSEThe arguments (sex=sex, age=age, income=income, minor=minor) assign the same names to the columns as I originally named the vectors; I could just as easily call them anything. For instance,
> data.frame(a=sex, b=age, c=income, minor=minor) a b c minor 1 male 68 150 FALSE 2 male 48 21 FALSE 3 female 68 58 FALSE 4 female 27 124 FALSE 5 female 84 103 FALSE 6 male 92 112 FALSE 7 male 35 65 FALSE 8 female 15 117 TRUE 9 male 89 95 FALSE 10 male 26 54 FALSEBut I prefer the more descriptive labels I gave previously.
> population sex age income minor 1 male 68 150 FALSE 2 male 48 21 FALSE 3 female 68 58 FALSE 4 female 27 124 FALSE 5 female 84 103 FALSE 6 male 92 112 FALSE 7 male 35 65 FALSE 8 female 15 117 TRUE 9 male 89 95 FALSE 10 male 26 54 FALSENow let's say we want to order by the age of the people. To do that is a one liner:
> population[order(population$age),] sex age income minor 8 female 15 117 TRUE 10 male 26 54 FALSE 4 female 27 124 FALSE 7 male 35 65 FALSE 2 male 48 21 FALSE 1 male 68 150 FALSE 3 female 68 58 FALSE 5 female 84 103 FALSE 9 male 89 95 FALSE 6 male 92 112 FALSEThis is not magic; you can select arbitrary rows from any data frame with the same syntax:
> population[c language="(1,2,3),"][/c] sex age income minor 1 male 68 150 FALSE 2 male 48 21 FALSE 3 female 68 58 FALSEThe order function merely returns the indices of the rows in sorted order.
> order(population$age) [1] 8 10 4 7 2 1 3 5 9 6Note the $ syntax; you select columns of a data frame by using a dollar sign and the name of the column. You can retrieve the names of the columns of a data frame with the names function.
> names(population) [1] "sex" "age" "income" "minor" > population$income [1] 150 21 58 124 103 112 65 117 95 54 > income [1] 150 21 58 124 103 112 65 117 95 54As you can see, they are exactly the same.
So what we're really doing with the command
population[order(population$age),]is
population[c language="(8,10,4,7,2,1,3,5,9,6),"][/c]Note the trailing comma; what this means is to take all the columns. If we only wanted certain columns, we could specify after this comma.
> population[order(population$age),c(1,2)] sex age 8 female 15 10 male 26 4 female 27 7 male 35 2 male 48 1 male 68 3 female 68 5 female 84 9 male 89 6 male 92Related
ShareTweet To leave a comment for the author, please follow the link and comment on their blog: Developmentality » R. R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job. Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.Tag » How To Sort In R
-
Sorting Data - Quick-R
-
Sorting In R Using Order() Tutorial - DataCamp
-
How Can I Sort My Data In R? | R FAQ - Statistical Consulting
-
R Sort A Data Frame Using Order() - Guru99
-
How To Sort A Data Frame By Multiple Columns In R - Chartio
-
How To Sort A DataFrame In R ? - GeeksforGeeks
-
Sort Function - RDocumentation
-
SORT In R With Sort() And Order() Functions [vectors, Data Frames, ...]
-
How To Sort An R Data Frame (multiple Ways, Multiple Columns)
-
Sorting Or Ordering Vectors - R
-
Sort In R: How To Sort Data In R - R-Lang
-
How To Sort Data In R - ProjectPro
-
Sort Table In R (3 Examples) - Statistics Globe
-
Chapter 16 Arranging (Sorting) Data | R For HR: An Introduction To ...