I have a dataframe in R, and I am trying to set all cells in the form of a vector, either c(1,2,3) or 1:2 to NA. Is there any easy way to do this?
Asked
Active
Viewed 59 times
1
-
Please add data using `dput` and not as images. Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Jul 30 '20 at 02:29
1 Answers
1
You can use lengths
to count number of elements in each value of column. Set them to NA
where the length is greater than 1. Here I am considering dataframe name as df
and column name as col_name
. Change them according to your data.
df$col_name[lengths(df$col_name) > 1] <- NA

Ronak Shah
- 377,200
- 20
- 156
- 213