0

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

1 Answer 1

0
others <- c("ID", "Start", "Complete", "IPAddress", "FirstName", "NumID", "Organization")

# add the first seven
colnames(data_process)[1:7] <- others

overarching <- grepl("Overarching", code_book$NewVariable)

# add overarching
colnames(data_process)[overarching] <- code_book$NewVariable[overarching]

breakfast <- grepl("Breakfast", code_book$NewVariable)

# add breakfast
colnames(data_process)[breakfast] <- code_book$NewVariable[breakfast]

Output:

> colnames(data_process)
  [1] "ID"                              "Start"                          
  [3] "Complete"                        "IPAddress"                      
  [5] "FirstName"                       "NumID"                          
  [7] "Organization"                    "X8"                             
  [9] "X9"                              "X10"                            
 [11] "OverarchingImproveRelationships" "OverarchingNewRelationships"    
 [13] "OverarchingTrust"                "OverarchingKnowledge"           
 [15] "OverarchingReferral"             "OverarchingConnections"         
 [17] "OverarchingSupport"              "OverarchingBest"                
 [19] "OverarchingChanges"              "X20"                            
 [21] "X21"                             "X22"                            
 [23] "X23"                             "X24"                            
 [25] "X25"                             "X26"                            
 [27] "X27"                             "X28"                            
 [29] "X29"                             "X30"                            
 [31] "X31"                             "X32"                            
 [33] "X33"                             "X34"                            
 [35] "X35"                             "X36"                            
 [37] "X37"                             "X38"                            
 [39] "X39"                             "X40"                            
 [41] "X41"                             "X42"                            
 [43] "X43"                             "X44"                            
 [45] "X45"                             "X46"                            
 [47] "X47"                             "X48"                            
 [49] "X49"                             "X50"                            
 [51] "X51"                             "X52"                            
 [53] "X53"                             "X54"                            
 [55] "X55"                             "X56"                            
 [57] "X57"                             "X58"                            
 [59] "X59"                             "X60"                            
 [61] "X61"                             "X62"                            
 [63] "X63"                             "X64"                            
 [65] "X65"                             "X66"                            
 [67] "X67"                             "X68"                            
 [69] "BreakfastAttend"                 "BreakfastMeet"                  
 [71] "BreakfastDiscuss"                "BreakfastLearn"                 
 [73] "BreakfastShare"                  "BreakfastNew"                   
 [75] "BreakfastBest"                   "BreakfastChange"                
 [77] "BreakfastUnable"                 "BreakfastUnable1"               
 [79] "BreakfastUnable2"                "BreakfastUnable3"               
 [81] "BreakfastUnable4"                "BreakfastUnable5"               
 [83] "BreakfastUnableOther"            "X84"                            
 [85] "X85"                             "X86"                            
 [87] "X87"                             "X88"                            
 [89] "X89"                             "X90"                            
 [91] "X91"                             "X92"                            
 [93] "X93"                             "X94"                            
 [95] "X95"                             "X96"                            
 [97] "X97"                             "X98"                            
 [99] "X99"                             "X100"                           
[101] "X101"                            "X102"                           
[103] "X103"                            "X104"                           
[105] "X105"                            "X106"                           
[107] "X107"                            "X108"                           
[109] "X109"                            "X110"                           
[111] "X111"                            "X112"                           
[113] "X113"                            "X114"                           
[115] "X115"                            "X116"                           
[117] "X117"                            "X118"                           
[119] "X119"                            "X120"                           
[121] "X121"                            "X122"                           
[123] "X123"                            "X124"                           
[125] "X125"                            "X126"                           
[127] "X127"                            "X128"                           
[129] "X129"                            "X130"                           
[131] "X131"                            "X132"                           
[133] "X133"                            "X134"                           
[135] "X135"                            "X136"                           
[137] "X137"                            "X138"                           
[139] "X139"                            "X140"                           
[141] "X141"                            "X142"                           
[143] "CardEmail"                      
3
  • Thank you very much Mark How to do so with no spaces between columns? First Overarching should be at X8 and the first breakfast column should be just next to the last Overarching. @Mark
    – Humberto R
    Commented Aug 11, 2023 at 13:46
  • are you wanting to drop the other columns? Or are you wanting to name the first n columns, where n is length(others) + number of Overarching columns + number of Breakfast columns
    – Mark
    Commented Aug 11, 2023 at 13:58
  • Correct Mark, the second option. First the N columns, then the Overarching columns, and then the Breakfast column. Surely I would be adding more that start with another word, and so on.
    – Humberto R
    Commented Aug 11, 2023 at 17:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.