英文:
Renaming values in a dataframe if there is a missing character in R
问题
我有一个数据框:
example <- data.frame(
Event = c('Negative_other 6',
'Negative_other_7',
'Neutral_self_1',
'Negative_other_6',
'Neutral_self 1',
'Negative_other_7'),
Type = c(1,2,2,1,2,2)
)
Event
列中的一些值的标签不一致。例如,'Negative_other_6' 和 'Neutral_self_1' 有时在 'other' 和数字之间有一个下划线,但有时没有。其他一些值的标签一直是一致的,比如 'Negative_other_7' 总是在 'other' 和数字之间有一个下划线。
我想要更新 Event
列中的所有值,以确保 'other' 和数字之间始终有一个下划线。
这是期望的输出:
solution <- data.frame(
Event = c('Negative_other_6',
'Negative_other_7',
'Neutral_self_1',
'Negative_other_6',
'Neutral_self_1',
'Negative_other_7'),
Type = c(1,2,2,1,2,2)
)
有人知道如何高效地实现这个吗?
谢谢!
英文:
I have a dataframe:
example <- data.frame(
Event = c('Negative_other 6',
'Negative_other_7',
'Neutral_self_1',
'Negative_other_6',
'Neutral_self 1',
'Negative_other_7'),
Type = c(1,2,2,1,2,2)
)
Some of the values in Event
are inconsistently labelled. For example, 'Negative_other_6' and 'Neutral_self_1' sometimes has an underscore between 'other' and the number but sometimes not. Some other values are consistently labelled, like 'Negative_other_7' always has an underscore between 'other' and the number.
I want to update all the values in Event
such that there is always an underscore between other and the number.
This is the desired output:
solution <- data.frame(
Event = c('Negative_other_6',
'Negative_other_7',
'Neutral_self_1',
'Negative_other_6',
'Neutral_self_1',
'Negative_other_7'),
Type = c(1,2,2,1,2,2)
)
Does anyone know an efficient way to do this?
Thank you!
答案1
得分: 1
你可以使用chartr
(将' '
转换为_
):
transform(example, Event = chartr(" ", "_", Event))
或者使用gsub
来替换' '
为_
:
transform(example, Event = gsub(" ", "_", Event))
使用tidyverse:
library(dplyr)
example %>%
mutate(Event = janitor::make_clean_names(Event))
英文:
You can use chatr
(character translate from ' '
to _
transform(example, Event = chartr(" ", "_", Event))
Event Type
1 Negative_other_6 1
2 Negative_other_7 2
3 Neutral_self_1 2
4 Negative_other_6 1
5 Neutral_self_1 2
6 Negative_other_7 2
or even gsub
for substituting ' '
to _
transform(example, Event = gsub(" ", "_", Event))
Event Type
1 Negative_other_6 1
2 Negative_other_7 2
3 Neutral_self_1 2
4 Negative_other_6 1
5 Neutral_self_1 2
6 Negative_other_7 2
Using tidyverse:
library(dplyr)
example %>%
mutate(Event = janitor::make_clean_names(Event))
Event Type
1 negative_other_6 1
2 negative_other_7 2
3 neutral_self_1 2
4 negative_other_6_2 1
5 neutral_self_1_2 2
6 negative_other_7_2 2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论