I have some values in a dataframe called ‘values’
ie:
Sam Ned Ted Ann
500 430 57 212
410 265 69 341
189 554 153 457
236 311 590 271
50 568 234 442
And some quality control flags in a separate dataframe ‘flags’
Sam$F Ned$F Ted$F Ann$F
1 1 1 0
0 0 1 0
0 0 1 0
0 0 0 0
0 0 1 0
I would like to replace the number in ‘values’ with NA if the equivalent entry in flags is a 1 i.e. resulting in the following
Sam Ned Ted Ann
NA NA NA 212
410 265 NA 341
189 554 NA 457
236 311 590 271
50 568 NA 442
This question is very similar to this one: Replacing certain values in data.frame in R
Which has been solved neatly with a ‘merge tables’ solution.
Except that I have a lot of data columns to perform this on, not just one. However I should be able to get the same ‘merge tables’ solution to work for me.
To merge the 2 tables (and replace 1s with NA I have used the following
F2 <-Flags
F2[F2 == "1"] <- "NA"
# Create identical column names for values and F2 for matching
samples <-colnames(Rvalues)
colnames(F2) <-samples
#Create an ID column for F2 and values
F2$ID <- c(1,2,3,4,5)
Rvalues$ID <- c(1,2,3,4,5)
out2 <- merge(Rvalues, F2, by = c("ID"), all.x = TRUE)
The resulting out2 dataframe is as follows:
ID Sam.x Ned.x Ted.x Ann.x Sam.y Ned.y Ted.y Ann.y
1 1 500 430 57 212 NA NA NA 0
2 2 410 265 69 341 0 0 NA 0
3 3 189 554 153 457 0 0 NA 0
4 4 236 311 590 271 0 0 0 0
5 5 50 568 234 442 0 0 NA 0
So I would like to use the solution posted (by Chase) in the link above, within a loop to go through the columns for each sample.
I have tried the following:
for [i in samples]
{
out3 <- transform(out2, [i]$new = ifelse(is.na([i].x), [i].y, [i].x), [i].x = NULL, [i].y = NULL)}
It is not working (error message
"Error: unexpected '[' in:
"{
out3 <- transform(out2, newdata = ifelse(is.na(["")
So I have something wrong, without the [] brackets it also does not work.
Any fixes greatly appreciated, many thanks
If your ‘values’ and ‘flags’ data.frames have the same dimension and order of columns