I have a (large) data frame as follows:
library(data.table)
DT <- fread(
"ID country year A b B a
4 NLD 2002 NA 1 NA 0
5 NLD 2002 NA 0 NA 1
6 NLD 2006 NA 1 NA 1
7 NLD 2006 NA 0 NA 0
8 NLD 2006 0 NA 0 NA
9 GBR 2002 0 NA 0 NA
10 GBR 2002 0 NA 0 NA
11 GBR 2002 0 NA 0 NA
12 GBR 2006 1 NA 1 NA
13 GBR 2006 1 NA 0 NA",
header = TRUE)
I would simply like to merge variables A
and a
, and, B
and b
.
EDIT: The problem is that I have to do this for more than 1000 variables, so I would like to avoid specifying either the column names which need not to be checked or the ones that do.
I was hoping for a solution that first splits the columns into a group for which there is no non-capitalised alternative, and a group for which there is.
As far as I understand the solution here:
Coalesce columns based on pattern in R
It still requires to provide the variables names for which the case needs to be checked. If I misunderstand this solution, which is very much possible, please let me know. In any case, as explained, I need a solution without specifically specifying the variables.
I found a good start here.
That solution has however a slightly different approach than the one I need.
How do I make such a variable merge conditional on something like tolower(varname) == varname
?
Desired output:
DT <- fread(
"ID country year A B
4 NLD 2002 0 1
5 NLD 2002 1 0
6 NLD 2006 1 1
7 NLD 2006 0 0
8 NLD 2006 0 0
9 GBR 2002 0 0
10 GBR 2002 0 0
11 GBR 2002 0 0
12 GBR 2006 1 1
13 GBR 2006 1 0 ",
header = TRUE)