提问人:Humberto R 提问时间:8/11/2023 最后编辑:Humberto R 更新时间:8/11/2023 访问量:41
R 字符串列匹配和添加
R String column matching and adding
问:
我有一个名为 code_book 的数据集,这个数据集有一个名为 New Variable 的列。此列的内容是字符串。我希望这些字符串是另一个数据集的列名,即 data_process。
但是,我只想传递code_book以“总体”一词开头的字符串。有 9 个字符串以该名称开头。
它们不应添加到同一索引处,而应仅添加到 NA 值或零的 data_process 中的下一列。
在此之后,第二个任务是现在添加以 Breakfast 开头的字符串,即 8。这些需要添加到以“总体”开头的最后一列旁边
我首先需要添加前 7 个,分别是 ID、Start、Complete、IPAddress、FirstName、NumID 和 Organization。然后,以总体开头的那些,然后是以早餐开头的那些
我已经尝试了这 2 行代码,但它们不起作用。
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]
}
}
另外,我尝试过这个,但只是为了第一个词 Overarching。
ifelse(grepl("Overarching", code_book$NewVariable),
colnames(data_process)[x] <- code_book$NewVariable[x],
code_book)
关于我怎样才能做到这一点的任何想法?
以下是 2 个数据集。
https://drive.google.com/drive/folders/1xpbR1GaOSi8qSOFS7rhfZKuI-jdYuSsq?usp=sharing
答:
0赞
Mark
8/11/2023
#1
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]
输出:
> 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"
评论