在R数据框中如何只分割列中的特定因子?

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

How to divide only a certain factor in a column in R data frame?

问题

这是示例数据集:

  1. library(data.table)
  2. df = data.table(x = c(1000,2000,10,2), y = c('A','A','B','B'))

我只想将 df$y == "A" 除以 1000。最终数据集应如下所示:

  1. df = data.table(x = c(1,2,10,2), y = c('A','A','B','B'))
英文:

This is the sample dataset:

  1. library(data.table)
  2. df = data.table(x = c(1000,2000,10,2), y = c('A','A','B','B'))

I only want to divide df$y == "A" by 1000. The final dataset should appear as:

  1. df = data.table(x = c(1,2,10,2), y = c('A','A','B','B'))

答案1

得分: 3

在基本的R中:

  1. df$x <- ifelse(df$y == "A", df$x/1000, df$x)

dplyr中:

  1. library(dplyr)
  2. df <- df %>%
  3. mutate(x = if_else(y == "A", x/1000, x))
英文:

You need to create a conditional statement.

In base R:

  1. df$x &lt;- ifelse(df$y == &quot;A&quot;, df$x/1000, df$x)

In dplyr:

  1. library(dplyr)
  2. df &lt;- df |&gt;
  3. mutate(x = if_else(y == &quot;A&quot;, x/1000, x))

答案2

得分: 2

  1. 使用 `data.table` 选项,像这样使用 `fifelse`
  2. ``` r
  3. library(data.table)
  4. df = data.table(x = c(1000,2000,10,2), y = c('A','A','B','B'))
  5. df[,x:=fifelse(y == "A", x/1000, x),]
  6. df
  7. #> x y
  8. #> 1: 1 A
  9. #> 2: 2 A
  10. #> 3: 10 B
  11. #> 4: 2 B

2023-02-18 创建,使用 reprex v2.0.2

  1. <details>
  2. <summary>英文:</summary>
  3. `data.table` option using `fifelse` like this:
  4. ``` r
  5. library(data.table)
  6. df = data.table(x = c(1000,2000,10,2), y = c(&#39;A&#39;,&#39;A&#39;,&#39;B&#39;,&#39;B&#39;))
  7. df[,x:=fifelse(y == &quot;A&quot;, x/1000, x),]
  8. df
  9. #&gt; x y
  10. #&gt; 1: 1 A
  11. #&gt; 2: 2 A
  12. #&gt; 3: 10 B
  13. #&gt; 4: 2 B

<sup>Created on 2023-02-18 with reprex v2.0.2</sup>

答案3

得分: 2

  1. 我们可以使用 `data.table` 的方法,因为输入是一个 `data.table`

library(data.table)
df[y == 'A', x := x/1000]

  1. -输出

> df
x y
1: 1 A
2: 2 A
3: 10 B
4: 2 B

  1. <details>
  2. <summary>英文:</summary>
  3. We could use `data.table` methods as the input is a `data.table`

library(data.table)
df[y == 'A', x := x/1000]

  1. -output

> df
x y
1: 1 A
2: 2 A
3: 10 B
4: 2 B

  1. </details>
  2. # 答案4
  3. **得分**: 1

基础R:使用[进行子集提取:

  1. df$x[df$y == "A"] <- df$x[df$y == "A"]/1000
  1. x y
  2. 1: 1 A
  3. 2: 2 A
  4. 3: 10 B
  5. 4: 2 B
英文:

Base R: Subsetting with [:

  1. df$x[df$y == &quot;A&quot;] &lt;- df$x[df$y == &quot;A&quot;]/1000
  1. x y
  2. 1: 1 A
  3. 2: 2 A
  4. 3: 10 B
  5. 4: 2 B

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

发表评论

匿名网友

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

确定