I have a dataset called code_book, and this dataset has a column named New Variable. The content of this column is strings. I want these strings to be the column names of another dataset, which is data_process.
But, I just want to pass the string from code_book that just begins with the word Overarching. There are 9 strings that start with that name.
They should not be added at the same index, but just the next column in data_process with NA Value or Zero.
After this, the second task is to add now the strings that start with Breakfast, which are 8. These need to be added next to the last column that starts with Overarching
I first need to add the first 7, which are ID, Start, Complete, IPAddress, FirstName, NumID, and Organization. Then, the ones that start with Overarching and then the ones that start with Breakfast
I have tried these 2 lines of code, but they are not working.
1.
for (x in 1:length(code_book$NewVariable)) {
if (str_starts(code_book$NewVariable[x], "Overarching")) {
colnames(data_process)[x] = code_book$NewVariable[x]
} else if (str_starts(code_book$NewVariable[x], "Breakfast")) {
colnames(data_process)[x] = code_book$NewVariable[x]
}
}
Also, I tried this, but just for the first word Overarching.
ifelse(grepl("Overarching", code_book$NewVariable),
colnames(data_process)[x] <- code_book$NewVariable[x],
code_book)
Any ideas of how can I accomplish this?
Here are the 2 datasets.
https://drive.google.com/drive/folders/1xpbR1GaOSi8qSOFS7rhfZKuI-jdYuSsq?usp=sharing