我有一个数据框,其中包含名为ProjectSubject的列。数据框大约有100万行。
在ProjectSubject列中,我有许多不同的字符串。以下是一个示例:
>unique(unlist(projectdf$ProjectSubject))
[1] "Applied Learning" "Applied Learning, Literacy
& Language"
[3] "Literacy & Language" "Special Needs"
[5] "Literacy & Language, History & Civics" "Math & Science"
[7] "History & Civics, Math & Science" "Literacy & Language,
Special Needs"
[9] "Applied Learning, Special Needs" "Health & Sports, Special
Needs"
[11] "Math & Science, Literacy & Language" "Literacy & Language, Math
& Science"
[13] "Literacy & Language, Music & The Arts" "Math & Science, Special
Needs"
[15] "Health & Sports" "Music & The Arts"
[17] "Math & Science, Applied Learning" "Literacy & Language,
Applied Learning"
[19] "Applied Learning, Music & The Arts" "History & Civics,
Literacy & Language"
[21] "Applied Learning, Math & Science" "Health & Sports, Math &
Science"
[23] "Applied Learning, Health & Sports" "History & Civics"
[25] "History & Civics, Music & The Arts" "Math & Science, History &
Civics"
[27] "Math & Science, Music & The Arts" "Special Needs, Music &
The Arts"
[29] "History & Civics, Applied Learning" "History & Civics, Special
Needs"
我需要一种简明的非手动方式来遍历数据框中的整个列,并用不同的字符串替换其中的一些字符串。例如,我想将“Applied Learning,Special Needs”替换为“Special Needs”,或者类似地将“Applied Learning,Math & Science”替换为“Math”。
我有大约50个唯一的字符串,就像上面给出的示例代码一样,我想将它们缩减为大约10个唯一的字符串。最好有一种方法,我不需要为这50个字符串中的每一个手动键入一行代码。