在基本的R中,是否可以使用字典的方式将多个整数替换为相应的字符串?

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

Is it possible in base R to replace multiple integers with corresponding strings like a dictionary?

问题

类似于此问题:https://stackoverflow.com/questions/63173687/replace-multiple-values-in-r 但是:我只想使用基本的R,并且我想替换整数,因此下面采用的命名向量解决方案不起作用:

  1. testing <- data.frame(
  2. var1 = c(1, 6, 17)
  3. )
  4. # 这是不可能的
  5. dict <- c(
  6. 1 = 'ICMP',
  7. 6 = 'TCP',
  8. 17 = 'UDP',
  9. )
  10. testing$var1 <- dict[testing$var1]

我知道可以这样做

  1. testing$var1[testing$var1 == 1] <- "ICMP"
  2. testing$var1[testing$var1 == 6] <- "TCP"
  3. testing$var1[testing$var1 == 17] <- "UDP"

但是在基本R中是否有更方便的方法?

英文:

Similar to this question: https://stackoverflow.com/questions/63173687/replace-multiple-values-in-r but: I would like to only use base R and I would like to replace integers, so the named vector solution adopted as below does not work:

  1. testing &lt;- data.frame(
  2. var1 = c(1, 6, 17)
  3. )
  4. # this is not possible
  5. dict &lt;- c(
  6. 1 = &#39;ICMP&#39;,
  7. 6 = &#39;TCP&#39;,
  8. 17 = &#39;UDP&#39;,
  9. )
  10. testing$var1 &lt;- dict[testing$var1]

I know I can do

  1. testing$var1[testing$var1 == 1] &lt;- &quot;ICMP&quot;
  2. testing$var1[testing$var1 == 6] &lt;- &quot;TCP&quot;
  3. testing$var1[testing$var1 == 17] &lt;- &quot;UDP&quot;

but isn't there a more convenient way in base R?

答案1

得分: 2

创建命名向量字典

dict <- c('ICMP', 'TCP', 'UDP')
names(dict) <- c(1, 6, 17)

替换匹配的值

testing$var2 <- dict[as.character(testing$var1)]

var1 var2

1 1 ICMP

2 6 TCP

3 17 UDP

英文:
  1. # create named vector dict
  2. dict &lt;- c(&#39;ICMP&#39;, &#39;TCP&#39;, &#39;UDP&#39;)
  3. names(dict) &lt;- c(1,6,17)
  4. # replace matching values
  5. testing$var2 &lt;- dict[as.character(testing$var1)]
  6. # var1 var2
  7. # 1 1 ICMP
  8. # 2 6 TCP
  9. # 3 17 UDP

答案2

得分: 2

  1. testing <- data.frame(
  2. var1 = c(1, 6, 17)
  3. )
  4. dict <- data.frame(code = c(1, 6, 17), abbr = c('ICMP', 'TCP', 'UDP'))
  5. merge(testing, dict, by.x = "var1", by.y = "code")
  6. # var1 abbr
  7. #1 1 ICMP
  8. #2 6 TCP
  9. #3 17 UDP
  10. testing$var1 <- merge(testing, dict, by.x = "var1", by.y = "code")$abbr
英文:
  1. testing &lt;- data.frame(
  2. var1 = c(1, 6, 17)
  3. )
  4. dict &lt;- data.frame(code = c(1, 6, 17), abbr = c(&#39;ICMP&#39;, &#39;TCP&#39;, &#39;UDP&#39;))
  5. merge(testing, dict, by.x = &quot;var1&quot;, by.y = &quot;code&quot;)
  6. # var1 abbr
  7. #1 1 ICMP
  8. #2 6 TCP
  9. #3 17 UDP
  10. testing$var1 &lt;- merge(testing, dict, by.x = &quot;var1&quot;, by.y = &quot;code&quot;)$abbr

答案3

得分: 2

以下是翻译好的部分:

有多种选项。首先,您可以使用字符串作为索引:

  1. dict <- c("1" = "ICMP", "6" = "TCP")
  2. dict[as.character(testing$var)]

您也可以通过执行 names(dict) <- c(1, 6) 来实现相同的效果。

然后,您可以使用反向哈希:

  1. dict <- c(ICMP = 1, TCP = 6)
  2. names(dict)[match(testing$var1, dict)]
英文:

There is a number of options. First, you can use strings as indices:

  1. dict &lt;- c(&quot;1&quot; = &quot;ICMP&quot;, &quot;6&quot; = &quot;TCP&quot;)
  2. dict[ as.character(testing$var) ]

You can achieve the same by doing names(dict) &lt;- c(1, 6).

Then, you can use a reverse hash:

  1. dict &lt;- c(ICMP = 1, TCP = 6)
  2. names(dict)[ match(testing$var1, dict) ]

huangapple
  • 本文由 发表于 2023年1月9日 17:02:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055026.html
匿名

发表评论

匿名网友

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

确定