1

I have a like this ,

AID=c("9671608","9671618","9677162")
dep=c(23,45,12)
t8=c(1,0,2)
a1_sum=c(2,10,1)

dataall=data.frame(AID,dep,t8,a1_sum)
    > dataall
      AID dep t8 a1_sum
1 9671608  23  1      2
2 9671618  45  0      10
3 9677162  12  2      1

I modified the value of a1_sum at the second row as follows,

dataall$a1_sum[dataall$AID=="9671618"]=1

I did it sucessfully, but I am getting following warning message, even at times when I run some other different code related to this data as follows:

Warning messages: 1: Unknown or uninitialised column: 'a1_sum'.

What may be the reason for this?

Thank you.

student_R123
  • 962
  • 11
  • 30
  • `al_sum` is different than `a1_sum`. Check your code to see where you've typed the lowercase `l` instead of the number `1`. – Len Greski Mar 28 '20 at 18:43
  • 1
    @r2evans - One of the things that is tricky about this code is that `dataall$AID=="9671618"` is used to subset a vector, `dataall$a1_sum[]`, instead of a data frame. – Len Greski Mar 28 '20 at 18:55
  • Crap, you're right ... incomplete reading. (It still looks like a typo-induced problem.) – r2evans Mar 28 '20 at 18:59
  • @LenGreski thank you for the comment. This code is actually a reproducible code that i developed to demonstrate my actual problem.Seems there is no typo like you mentioned . Also i think this issue may be due to the way i subset the data . Is there more efficient way to do this other than my method ? – student_R123 Mar 28 '20 at 19:14
  • Did you actually set the column `a1_sum` before doing `dataall$a1_sum[dataall$AID=="9671618"]=1`? That can sometimes cause problems [check this link.](https://stackoverflow.com/questions/39041115/fixing-a-multiple-warning-unknown-column) Appart from that I do not see anything wrong with your code. – niko Mar 28 '20 at 21:41

1 Answers1

1

I would suggest the following :

EDIT : as @r2evans, rightfully pointed out it is best to use if_else rather than case_when when there is only one condition:

library(dplyr)
dataall <- dataall %>% dplyr::mutate(a1_sum = dplyr::if_else(AID == "9671618", 1, a1_sum)

or data.table solution:

library(data.table)
dataall <- data.table::setDT(dataall)
dataall[AID =="9671618", a1_sum :=  1]

Raphaele Adjerad
  • 1,117
  • 6
  • 12
  • 1
    When there's a single conditional, I'd strongly recommend `if_else` over `case_when`, as it should be much faster. I think `case_when` wins out when the number of tests increase, analogous to multiple nested `if_else` calls. – r2evans Mar 28 '20 at 23:30