Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping low occuring levels in a dataframe in R

Tags:

r

Suppose that I have a data frame that has a column called C. C has many levels that only occur once. How would I rename all of the levels that occur only once with a new level (called z)?

A  B  C   
a  a  a  
a  b  b  
a  a  c  
a  b  d  
a  b  a  

The above would turn into:

A  B  C   
a  a  a  
a  b  z  
a  a  z  
a  b  z  
a  b  a 
like image 335
kevin ko Avatar asked Jul 23 '15 18:07

kevin ko


People also ask

How to group Dataframe by multiple columns in R?

Groupby Function in R – group_by is used to group the dataframe in R. Dplyr package in R is provided with group_by () function which groups the dataframe by multiple columns with mean, sum and other functions like count, maximum and minimum.

How do you reduce the number of groups in a Dataframe?

The number of groups may be reduced, based on conditions. dataframe attributes are preserved during data filter. Any dataframe column in the R programming language can be referenced either through its name df$col-name or using its index position in the dataframe df [col-index].

How do I Group and summarize data in R?

Two of the most common tasks that you’ll perform in data analysis are grouping and summarizing data. Fortunately the dplyr package in R allows you to quickly group and summarize data. This tutorial provides a quick guide to getting started with dplyr. Before you can use the functions in the dplyr package, you must first load the package:

How to find missing values in a Dataframe in R?

Cells in dataframe can contain missing values or NA as its elements, and they can be verified using is.na () method in R language. Column values can be subjected to constraints to filter and subset the data. The values can be mapped to specific occurrences or within a range.


1 Answers

What about this (assuming your data is df)?

levels(df[,3])[table(df[,3])==1] <- "z"
df
  A B C
1 a a a
2 a b z
3 a a z
4 a b z
5 a b a
like image 73
DatamineR Avatar answered Sep 23 '22 19:09

DatamineR