在R中,如果数据框中存在缺失字符,可以重命名数值。

huangapple go评论129阅读模式
英文:

Renaming values in a dataframe if there is a missing character in R

问题

我有一个数据框:

  1. example <- data.frame(
  2. Event = c('Negative_other 6',
  3. 'Negative_other_7',
  4. 'Neutral_self_1',
  5. 'Negative_other_6',
  6. 'Neutral_self 1',
  7. 'Negative_other_7'),
  8. Type = c(1,2,2,1,2,2)
  9. )

Event 列中的一些值的标签不一致。例如,'Negative_other_6' 和 'Neutral_self_1' 有时在 'other' 和数字之间有一个下划线,但有时没有。其他一些值的标签一直是一致的,比如 'Negative_other_7' 总是在 'other' 和数字之间有一个下划线。

我想要更新 Event 列中的所有值,以确保 'other' 和数字之间始终有一个下划线。

这是期望的输出:

  1. solution <- data.frame(
  2. Event = c('Negative_other_6',
  3. 'Negative_other_7',
  4. 'Neutral_self_1',
  5. 'Negative_other_6',
  6. 'Neutral_self_1',
  7. 'Negative_other_7'),
  8. Type = c(1,2,2,1,2,2)
  9. )

有人知道如何高效地实现这个吗?

谢谢!

英文:

I have a dataframe:

  1. example &lt;- data.frame(
  2. Event = c(&#39;Negative_other 6&#39;,
  3. &#39;Negative_other_7&#39;,
  4. &#39;Neutral_self_1&#39;,
  5. &#39;Negative_other_6&#39;,
  6. &#39;Neutral_self 1&#39;,
  7. &#39;Negative_other_7&#39;),
  8. Type = c(1,2,2,1,2,2)
  9. )

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:

  1. solution &lt;- data.frame(
  2. Event = c(&#39;Negative_other_6&#39;,
  3. &#39;Negative_other_7&#39;,
  4. &#39;Neutral_self_1&#39;,
  5. &#39;Negative_other_6&#39;,
  6. &#39;Neutral_self_1&#39;,
  7. &#39;Negative_other_7&#39;),
  8. Type = c(1,2,2,1,2,2)
  9. )

Does anyone know an efficient way to do this?

Thank you!

答案1

得分: 1

你可以使用chartr(将&#39; &#39;转换为_):

  1. transform(example, Event = chartr(" ", "_", Event))

或者使用gsub来替换&#39; &#39;_

  1. transform(example, Event = gsub(" ", "_", Event))

使用tidyverse:

  1. library(dplyr)
  2. example %>%
  3. mutate(Event = janitor::make_clean_names(Event))
英文:

You can use chatr (character translate from &#39; &#39; to _

  1. transform(example, Event = chartr(&quot; &quot;, &quot;_&quot;, Event))
  2. Event Type
  3. 1 Negative_other_6 1
  4. 2 Negative_other_7 2
  5. 3 Neutral_self_1 2
  6. 4 Negative_other_6 1
  7. 5 Neutral_self_1 2
  8. 6 Negative_other_7 2

or even gsub for substituting &#39; &#39; to _

  1. transform(example, Event = gsub(&quot; &quot;, &quot;_&quot;, Event))
  2. Event Type
  3. 1 Negative_other_6 1
  4. 2 Negative_other_7 2
  5. 3 Neutral_self_1 2
  6. 4 Negative_other_6 1
  7. 5 Neutral_self_1 2
  8. 6 Negative_other_7 2

Using tidyverse:

  1. library(dplyr)
  2. example %&gt;%
  3. mutate(Event = janitor::make_clean_names(Event))
  4. Event Type
  5. 1 negative_other_6 1
  6. 2 negative_other_7 2
  7. 3 neutral_self_1 2
  8. 4 negative_other_6_2 1
  9. 5 neutral_self_1_2 2
  10. 6 negative_other_7_2 2

huangapple
  • 本文由 发表于 2023年7月7日 00:19:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76630779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定