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: Copy df <- data.frame(product = c(“laptop”, “printer”, “tablet”, “desk”, “chair”), brand = c(“A”, “B”, “X”, “Y”, “Z”), price = c(1200, … Read more

How to Merge two DataFrames in R

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

Remove a Column in a DataFrame in R

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

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: Copy colnames(df)[colnames(df) == “old_column_name”] <- “new_column_name” By specifying the column index to rename a single column: Copy colnames(df)[column_index] <- “new_column_name” … Read more

Remove Duplicates from a Column in R DataFrame

Use the duplicated() function to remove duplicates from a column in R DataFrame: Copy df_unique <- df[!duplicated(df$column_name), ] Examples First, create a DataFrame in R with 2 columns that contain duplicates: Copy df <- data.frame(colors = c(“Red”, “Red”, “Red”, “Green”, “Green”, “Green”, “Blue”, “Blue”), numbers = c(1, 1, 2, 3, 3, 3, 4, 5) )print(df) … Read more

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: Copy df$new_column_name <- c(“value_1”, “value_2”, “value_3”, …) (2) Using cbind: Copy 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 … Read more

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: Copy df <- df[order(df$column_name), ] (2) Sort a DataFrame based on a single column in a descending order: Copy df <- df[order(df$column_name, decreasing=TRUE), ] Examples of Sorting a DataFrame in … Read more

How to Export a DataFrame to a Text File in R

To export a DataFrame to a text file in R: Copy write.table(df, “Path to export the DataFrame\\File Name.txt”) Add a separator, such as a comma separator: Copy write.table(df, “Path to export the DataFrame\\File Name.txt”, sep=”,”) Exclude the row numbers: Copy write.table(df, “Path to export the DataFrame\\File Name.txt”, row.names=FALSE) Omit the quotes: Copy write.table(df, “Path to … Read more

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: Copy df[is.na(df)] <- 0 (2) Replace NA values with zeros under a single DataFrame column: Copy df[“column_name”][is.na(df[“column_name”])] <- 0 In the following section, you’ll see how to apply the above … Read more

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: Copy df[df == “Old Value”] <- “New Value” (2) Replace a value under a single DataFrame column: Copy df[“Column Name”][df[“Column Name”] == “Old Value”] <- “New Value” Next, you’ll see 4 scenarios that describe how … Read more