I am working with a dataset that has many values listed as "<.1". We treat these values by entering in half of the value: 0.05, in this case. However not every cell in the dataset has these < signs so I can't just cut every value in the dataset in half. How do I conditionally edit the dataset to get change this?? Seems like an easy fix but I can't figure it out. Any help is much appreciated!!
df <- as.data.frame(matrix(c("0.09", "<.1", "40", "<.07", ".2", "376", "<0.075", "<0.01", "14"), ncol = 3, byrow = TRUE))
df
I want my data to look like this:
df1 <- as.data.frame(matrix(c("0.09", "0.05", "40", "0.035", ".2", "376", "0.0375", "0.005", "14"), ncol = 3, byrow = TRUE))
df1
EDIT: I have done this:
x <- data[2]
data[2:23] <- lapply(data[2:23], function(x) {
dohalf <- grepl("^<", x)
vec2 <- as.numeric(gsub("^<", "", x))
vec2[dohalf] <- vec2[dohalf]/2
})
but I get this error:
Error: Assigned data `lapply(...)` must be compatible with existing data.
x Existing data has 241 rows.
x Element 1 of assigned data has 71 rows.
i Only vectors of size 1 are recycled.
Is it because I have NA's? There are only 71 values that have this "<" in the first column out of 241 observations.