1

I have the following dataframe

enter image description here

And I want to replace the NA's with this vector (except it is class numeric for me, but I changed it to as.character() and still got the same error)

L8Replace <- c("0.56","0.57","0.87",0.81")

I have tried

  mydata[is.na(mydata$Freq2),] <- L8replace

But get

  Error in as.Date.numeric(value) : 'origin' must be supplied

I understand why R is confused, but I do not know how to fix it

For your use, this is what I get when I put my data though dput:

mydata <- structure(list(Freq2 = structure(c(0.42, 0.6, 0.43, NA, NA, NA, 
NA, NA), .Names = c("2015-04-16", "2015-04-21", "2015-04-26", 
"", "", "", "", "")), myDate = structure(c(16541, 16546, 16551, 
16627, 16643, 16659, 16675, 16691), class = "Date"), numDays = structure(c(0, 
5, 10, 86, 102, 118, 134, 150), class = "difftime", units = "days")), .Names =      c("Freq2", 
 "myDate", "numDays"), row.names = c("2015-04-16", "2015-04-21", 
"2015-04-26", "7", "8", "9", "10", "11"), class = "data.frame")
  • Possible duplicate of [R: replace NA with item from vector](http://stackoverflow.com/questions/6684695/r-replace-na-with-item-from-vector) – Ronak Shah Mar 20 '17 at 05:43

1 Answers1

0

We need to replace the specific column

mydata$Freq2[is.na(mydata$Freq2)] <- L8Replace
mydata
#            Freq2     myDate  numDays
#2015-04-16  0.42 2015-04-16   0 days
#2015-04-21  0.60 2015-04-21   5 days
#2015-04-26  0.43 2015-04-26  10 days
#7           0.56 2015-07-11  86 days
#8           0.57 2015-07-27 102 days
#9           0.87 2015-08-12 118 days
#10          0.81 2015-08-28 134 days
#11          0.00 2015-09-13 150 days

Also, the length of 'L8Replace' should match the number of NA in 'Freq2'

data

L8Replace <-  c(0.56, 0.57, 0.87,0.81, 0)
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662