How do you change specific values (like 7) in a column to NA in R? -
i working on project did not know recorded 7 in column , refused answer recorded 9. trying find easy way convert these values na.
you can use simple logic assign na, in following simple example.
column <- c(1,2,1,3,7,7,1,2,9) column[column %in% c(7,9)] <- na > column [1] 1 2 1 3 na na 1 2 na
basically, can operate on column of data frame using $ operator. treats column vector. using logical operator on vector returns vector of true or false back, can used select elements of vector change na.
i caution might not want that. using na instead of values in r can have annoying side effects because operation against na returns na.
edited add: per gregor, should mention data frame allows select single columns out of using $
operator. instance, in data frame df.example
, if columns a, b, , c, df.example$a
extract column vector. in contrast, [
operator not create vector subsets, , used select multiple columns of data frame smaller data frame. instance, given our example data frame, select columns , b different data frame using df.example[c("a","b")]
. more guidance, trying running help('[')
in r.
Comments
Post a Comment