Home > Mobile >  Replacing values of a column in R dataframe
Replacing values of a column in R dataframe

Time:12-09

I have a data frame named C0001 with 3671 observations of 31 variables. I want to apply a check on each value of one variable named Y. If the value of that variable is greater than 30, then replace it with 30 otherwise keep the existing value. I wrote the following in R but it gives me an error:

  C0001 <- read.csv("C0001.csv")
  C0001$Y<- ifelse(C0001$Y > 30, 30, C0001$Y)

Error in ans[npos] <- rep(no, length.out = len)[npos] : replacement has length zero In addition: Warning message: In rep(no, length.out = len) : 'x' is NULL so the result will be NULL

Could someone help me with what mistake I am making here? Is there some other way to do the same operation without using ifelse?

Thank you

CodePudding user response:

Use vectorization like this: C0001$Y <- C0001$Y[C0001$Y > 30]

CodePudding user response:

Try to replace read.csv() with read_csv() as well check your core work directory. The read_csv() function imports data into R as a tibble, while read.csv() imports a regular old R data frame instead. The error indicates that your input is either NULL or a length 0 vector: make sure the indices are correct.

library(readr)

C0001 <- read_csv("C:/Users/Desktop//C0001.csv")
C0001

> C0001
# A tibble: 6 x 3
      x     y     z
  <dbl> <dbl> <dbl>
1     2    40     4
2     3    12     5
3    45    12     6
4     1    50     7
5     1    50    30
6     1     0     0


C0001$y<- ifelse(C0001$y > 30, 30, C0001$y)
C0001

# A tibble: 6 x 3
      x     y     z
  <dbl> <dbl> <dbl>
1     2    30     4
2     3    12     5
3    45    12     6
4     1    30     7
5     1    30    30
6     1     0     0

Data sample:

structure(list(x = c(2, 3, 45, 1, 1, 1), y = c(30, 12, 12, 30, 
30, 0), z = c(4, 5, 6, 7, 30, 0)), row.names = c(NA, -6L), spec = structure(list(
    cols = list(x = structure(list(), class = c("collector_double", 
    "collector")), y = structure(list(), class = c("collector_double", 
    "collector")), z = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))
  • Related