Search code examples
rdataframeuniquena

How to delete a single value within a data.frame


I would like to delete a single value of a cell within a data.frame. The value is a factor (numeric)

I tried to access the value like this:

which(colnames(df) == "variable.name")
which(rownames(df) == "row.name")

df[i, j] <- NULL

I was also trying to produca a NA by <- is.na, this didn't work as well. How would I do this?


Solution

  • There are significant differences between NULL and NA.

    NULL is an object, typically used to mean the variable contains no object.

    NA is a value that typically means "missing data item here".

    In the main, a data frame is a list of equal length vectors. While an R list is an object that can contain other objects, an R vector is an object that can only contain values. Consequently, you can have a list of NULLs, but you cannot have a vector of NULLs.

    In your example, you are trying to put a NULL object into a vector object (df$j or df[,j] or df[[j]]), which is part of a data frame. As a vector object cannot contain other objects (only values) the assignment failed.

    As noted in the comments, df[i, j] <- NA will put an NA value in the data frame at the desired location. This is probably the best way to indicate emptyness.