Tuesday, June 20, 2023

x̄ - > Data cleaning in R

COMPUTING CATEGORY

 # Sample data frame

df <- data.frame(

  A = c(1, NA, 3, 4, 5),

  B = c("a", "b", NA, "d", "e"),

  C = c("x", "y", "z", NA, "w")

)


# Remove rows with missing values

df_without_na <- na.omit(df)


# Replace missing values with another value

df_replaced_na <- replace(df, is.na(df), "replacement_value")


# Remove duplicate rows

df_without_duplicates <- unique(df)


# Print the results

print("Data frame without missing values:")

print(df_without_na)


print("\nData frame with missing values replaced:")

print(df_replaced_na)


print("\nData frame without duplicate rows:")

print(df_without_duplicates)


licate rows:")
print(df_without_duplicates)
```


We create a sample data frame called `df` with three columns (`A`, `B`, and `C`). Each column contains some missing values represented by `NA`. 

Here's what each operation does:

1. Remove rows with missing values: We use the `na.omit()` function to remove any rows containing `NA` values and assign the result to a new data frame called `df_without_na`.

2. Replace missing values with another value: We use the `replace()` function to replace all `NA` values in the original data frame `df` with a specified replacement value (in this case, "replacement_value"). The result is stored in `df_replaced_na`.

3. Remove duplicate rows: We use the `unique()` function to remove duplicate rows from the original data frame `df` and store the result in `df_without_duplicates`.

Finally, we print the resulting data frames to see the output.

Note that the functions `na.omit()`, `replace()`, and `unique()` create new data frames and do not modify the original data frame unless you assign the result back to the same variable.
MAYBELLINE OFFICIAL STORE

No comments:

Meet the Authors
Zacharia Maganga’s blog features multiple contributors with clear activity status.
Active ✔
πŸ§‘‍πŸ’»
Zacharia Maganga
Lead Author
Active ✔
πŸ‘©‍πŸ’»
Linda Bahati
Co‑Author
Active ✔
πŸ‘¨‍πŸ’»
Jefferson Mwangolo
Co‑Author
Inactive ✖
πŸ‘©‍πŸŽ“
Florence Wavinya
Guest Author
Inactive ✖
πŸ‘©‍πŸŽ“
Esther Njeri
Guest Author
Inactive ✖
πŸ‘©‍πŸŽ“
Clemence Mwangolo
Guest Author

Followers

Support This Blog
Tap Donate now here to donate or go to donate on top menu to scan QR and support this site.
Donate Now