I have following data frame (named as 'df'), with columns names as ID, org1 and org2.
Structure:
ID org1 org2
1 C1 NA
2 C2 NA
3 C3 C3
4 C4 C4
5 C1 NA
6 NA C2
7 NA C4
8 NA NA
9 NA NA
10 C3 C3
Now, I want org1 to acquire values from org2 in case a 'NA' in org1 eg: ID 6
Expected output:
ID org1 org2
1 C1 NA
2 C2 NA
3 C3 C3
4 C4 C4
5 C1 NA
6 C2 C2
7 C4 C4
8 NA NA
9 NA NA
10 C3 C3
I tired the following:
df$org1[is.na(df$org1)] <- as.character(df$org2[is.na(df$org1)])
referring to the following: Replace NA in column with value in adjacent column
But this yielded the following error:
Warning message:
In `[<-.factor`(`*tmp*`, is.na(df$org1), :invalid factor level, NA generated
can someone tell me what I am doing wrong and is there a better solution for this.