Filter Rows Based on Conditions in a DataFrame in R

Here are several ways to filter rows based on conditions in a DataFrame in R: (1) Filter rows based on a single condition: To maintain only the rows where the price is greater than 400: df <- data.frame(product = c(“laptop”, “printer”, “tablet”, “desk”, “chair”), brand = c(“A”, “B”, “X”, “Y”, “Z”), price = c(1200, 150, … Read more

Categories R

How to Merge two DataFrames in R

Here are the different ways to merge two DataFrames in R: (1) Inner Join: inner_merged <- merge(df1, df2, by = “Id”) (2) Left Join: left_merged <- merge(df1, df2, by = “Id”, all.x = TRUE) (3) Right Join: right_merged <- merge(df1, df2, by = “Id”, all.y = TRUE) (4) Full Outer Join: outer_merged <- merge(df1, df2, … Read more

Categories R

Remove a Column in a DataFrame in R

Here are 3 ways to remove a single column in a DataFrame in R: Using subset() df <- subset(df, select = -column_name_to_remove) Using the indexing operator [] df <- df[, -which(names(df) == “column_name_to_remove”)] Using the column index: df <- subset(df, select = -column_index_to_remove) Here are additional 3 ways to remove multiple columns in a DataFrame … Read more

Categories R

Rename Columns in a DataFrame in R

Here are several ways to rename columns in a DataFrame in R: (1) Use the colnames() function to rename column/s in a DataFrame in R: By specifying the column name to rename a single column: colnames(df)[colnames(df) == “old_column_name”] <- “new_column_name” By specifying the column index to rename a single column: colnames(df)[column_index] <- “new_column_name” By specifying … Read more

Categories R

Remove Duplicates from a Column in a DataFrame in R

You may use the duplicated() function to remove duplicates from a column in a DataFrame in R: df_unique <- df[!duplicated(df$column_name), ] Examples of removing duplicates from a column in a DataFrame in R First, create a DataFrame in R with 2 columns that contain duplicates: df <- data.frame( colors = c(“Red”, “Red”, “Red”, “Green”, “Green”, … Read more

Categories R

Add a New Column to a DataFrame in R

Here are two ways to add a new column to a DataFrame in R: (1) Using the $ symbol: df$new_column_name <- c(“value_1”, “value_2”, “value_3”, …) (2) Using cbind: df <- cbind(df, new_column_name = c(“value_1”, “value_2”, “value_3”, …)) Examples of Adding a New Column to a DataFrame in R Example 1: Add a new column using … Read more

Categories R

How to Sort a DataFrame in R

The order() function can be used to sort a DataFrame in R: (1) Sort a DataFrame based on a single column in an ascending order: df[order(df$column_name), ] (2) Sort a DataFrame based on a single column in a descending order: df[order(df$column_name, decreasing=TRUE), ] In the examples below, you’ll also observe how to sort a DataFrame … Read more

Categories R

How to Export a DataFrame to a Text File in R

Here is the general syntax that you may use to export your DataFrame to a text file in R: write.table(DataFrame Name, “Path to export the DataFrame\\File Name.txt”) If you wish to add a separator, such as a comma separator, then use: write.table(DataFrame Name, “Path to export the DataFrame\\File Name.txt”, sep=”,”) You may exclude the row … Read more

Categories R

Replace NA Values with Zeros in DataFrame in R

Here are 2 ways to replace NA values with zeros in a DataFrame in R: (1) Replace NA values with zeros across the entire DataFrame: df[is.na(df)] <- 0 Note that if your DataFrame contains factors, you may consider adding “,stringsAsFactors = FALSE” at the end of your DataFrame (later you’ll see an example that tackles … Read more

Categories R

How to to Replace Values in a DataFrame in R

Here is the syntax to replace values in a DataFrame in R: (1) Replace a value across the entire DataFrame: df[df == “Old Value”] <- “New Value” (2) Replace a value under a single DataFrame column: df[“Column Name”][df[“Column Name”] == “Old Value”] <- “New Value” Next, you’ll see 4 scenarios that will describe how to: … Read more

Categories R