I realise I asked two separate questions in my last post and had one of them answered by very clever peeps super quickly.
Obviously I still can't wrap my head around data frame lists or lapply!
I have csv list of original questions and renamed questions. In this example, I am trying to write the code to update Q.1a to Q.1 as per the Qs data frame.
df1 <- data.frame("ID" = 1, "Q.1" = 2, Q1.1 = 3)
df2 <- data.frame("ID." = 2, "Q.1a" = 3, Q1.1 = 4)
dflist <- lapply(ls(), function(x) if (class(get(x)) == "data.frame") get(x))
dflist <- Filter(length, dflist)
Qs <- data.frame("Original.Name" = "Q.1a", "New.Name" = "Q.1")
The tables look like this: I want to update Q.1a as per the Qs table
ID Q.1a Q1.1
1 1 2 3
ID. Q.1 Q1.1
1 2 3 4
Original.Name New.name
1 Q.1a Q.1
The code I am trying to write to rename the questions that is currently full of errors, I am sure the piping is not supposed to be there!
lapply(dflist, function(x) {
names(x) <- names (x) %in%
Qs$Original.name = Qs$New.name[match(names(x)[names(x) %in% Qs$Original.name],
Qs$Original.name)]
})
Can anyone point me in the right direction? Thanks so much.
Edited to show expected output where Q1a from the original example above has been upated to Q1.
ID Q.1 Q1.1
1 1 2 3
ID. Q.1 Q1.1
1 2 3 4
Ideally I want to be able to match and replace the column names from the Qs table. The original column name replaced with new column name
dflisthas column namesID Q1 Q11andID Q1a Q11what should it change to?