I have a large data where I have to replace 'detected' from Organism variable into value from RESULT. I used the following code:
LAB$Organism[psie$Organism == 'DETECT'] <- LAB$RESULT
I got the following error: "NAs are not allowed in subscripted assignments".
I'm still new to R so I have no idea what is missing! Do I have to a code that take care of null values?
Any help is really appreciated.
Thank you
Is this the sort of thing you are trying to do?
LAB <- data.frame(Organism = c("foo","DETECT",NA,"bar"),
RESULT = c("A","B","C","D"))
LAB
#> Organism RESULT
#> 1 foo A
#> 2 DETECT B
#> 3 <NA> C
#> 4 bar D
LAB$Organism <- ifelse(LAB$Organism != 'DETECT', LAB$Organism, LAB$RESULT)
LAB
#> Organism RESULT
#> 1 foo A
#> 2 B B
#> 3 <NA> C
#> 4 bar D
Created on 2023-06-25 with reprex v2.0.2
It is unusual that you are subsetting the LAB data frame using a column from the psie data frame. That might be correct but you should check if that is what you want to do.
If the question is asked in those terms to a chatbot, an incorrect answer will be provided. A more careful question, using @FJCC 's data frame returns his result.
LAB <- data.frame(Organism = c("foo","DETECT",NA,"bar"),
RESULT = c("A","B","C","D"))
psie <- data.frame(Organism = c("foo","DETECT",NA,"bar"),
RESULT = c("A","Z","C","D"))
LAB$RESULT <- ifelse(LAB$Organism == "DETECT" & psie$Organism == "DETECT",
psie$RESULT,
LAB$RESULT)
LAB
#> Organism RESULT
#> 1 foo A
#> 2 DETECT Z
#> 3 <NA> <NA>
#> 4 bar D
Created on 2023-06-25 with reprex v2.0.2
Question posed:
In the R programming language, I have two large data frames that look like these examples
LAB <- data.frame(Organism = c("foo","DETECT",NA,"bar"),
RESULT = c("A","B","C","D"))
psie <- data.frame(Organism = c("foo","DETECT",NA,"bar"),
RESULT = c("A","B","Z","D"))
Write a script to replace the value of LAB$RESULT with psie$RESULT when the value "DETECT" appears in both LAB$Orgamism and psie$Organism. Asking for guidance on what to do, rather than how to do it or to correct the immediate mistake, is much more effective.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.