3

I am trying to replace all the negative values in a data frame with zero, and I also want to keep the original data frame with negative values. Here's my attempt with sample data:

col1 <- c("-1", "2", "-3", "0", "10")
col2<- c("1", "2", "3", "4", "5")
col3<- c("1", "-2", "3", "-4", "-5")
uncleaned.data <- data.frame(col1, col2, col3)

cleaned_data <- uncleaned.data[uncleaned.data<0]=0

I got an error like this object 'cleaned_data' not found Any suggestions? Intuitively I don't see any issues here .. Thanks a lot!

Paul726
  • 109
  • 6
  • 1
    I think the error would be because you havent assigned i.e. `cleaned_data <- uncleaned.data; cleaned.data[cleaned.data<0] <- 0`. Make sure that the columns are numeric instead of character – akrun Jun 08 '21 at 21:20
  • Did you intend for the sample data to be character and not numeric? –  Jun 08 '21 at 21:39

4 Answers4

3

In the code, there are two assignments. i.e. =0 is an assignment and clean_data is not initialized. We just need a copy of the original data as 'cleaned_data' and then do the replacement on that dataset

cleaned_data <- uncleaned.data
cleaned.data[cleaned.data < 0] <- 0
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    @AnoushiravanR perhaps `map_int(ls2, nrow) %>% setNames(seq_along(.)) %>% enframe %>% group_by(value) %>% summarise(out = list(bind_rows(ls2[as.integer(name)]))) %>% pull(out)` – akrun Jun 08 '21 at 21:49
  • 1
    compared to `split`, the other methods with `==` on `unique` etc would be less efficient though – akrun Jun 08 '21 at 21:57
3

This solution can also be used:

library(dplyr)

uncleaned.data %>% 
  rowwise() %>%
  mutate(across(everything(), ~ ifelse(as.numeric(.x) < 0, "0", .x)))

# A tibble: 5 x 3
# Rowwise: 
  col1  col2  col3 
  <chr> <chr> <chr>
1 0     1     1    
2 2     2     0    
3 0     3     3    
4 0     4     0    
5 10    5     0 
Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
2

It seems you need replace

cleaned_data <- replace(uncleaned.data, uncleaned.data < 0, 0)

such that

> cleaned_data
  col1 col2 col3
1    0    1    1
2    2    2    0
3    0    3    3
4    0    4    0
5   10    5    0
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
0
library(tidyverse)
library(stringr)

#preparing data
col1 <- c("-1", "2", "-3", "0", "10")
col2 <- c("1", "2", "3", "4", "5")
col3 <- c("1", "-2", "3", "-4", "-5")

uncleaned.data <- data.frame(col1, col2, col3)

#solution

uncleaned.data |> 
    map_dfr(~str_replace(.x, '-[0-9]+$', '0'))
#> # A tibble: 5 x 3
#>   col1  col2  col3 
#>   <chr> <chr> <chr>
#> 1 0     1     1    
#> 2 2     2     0    
#> 3 0     3     3    
#> 4 0     4     0    
#> 5 10    5     0

#optional

uncleaned.data |> 
    map_dfr(~str_replace(.x, '-[0-9]+$', '0')) %>% 
    mutate_all(as.numeric)
#> # A tibble: 5 x 3
#>    col1  col2  col3
#>   <dbl> <dbl> <dbl>
#> 1     0     1     1
#> 2     2     2     0
#> 3     0     3     3
#> 4     0     4     0
#> 5    10     5     0

Created on 2021-06-08 by the reprex package (v2.0.0)

jpdugo17
  • 6,816
  • 2
  • 11
  • 23