无法更改变量类型

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

Cannot change the variable type

问题

I can't understand why the type of the variable "condition" does not change with this code:

我不明白为什么变量"condition"的类型在这段代码中没有改变:

  1. df$condition <- as.character(df)
  2. df["condition"][df["condition"] == "1"] <- "passive"
  3. df["condition"][df["condition"] == "2"] <- "active"

I get this error message:

我收到以下错误消息:

  1. ! Assigned data `as.character(df)` must be compatible with existing data.
  2. x Existing data has 76 rows.
  3. x Assigned data has 3 rows.
  4. i Only vectors of size 1 are recycled.
  5. Backtrace:
  6. 1. base::`$<-`(`*tmp*`, condition, value = `<chr>`)
  7. 12. tibble (local) `<fn>`(`<vctrs___>`)

My data:

我的数据:

  1. structure(list(condition = c(2, 2, 1, 2, 2, 1, 1, 2, 2, 2, 1, 2, 2, 1, 2, 1, 2, 1, 1, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 1, 2, 1, 2, 1, 1, 2, 1, 1, 2, 2, 1, 2, 1, 1, 1, 2, 2, 1, 2, 1, 1, 2, 2, 1, 2, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 1, 2, 1), before= c(13, 7, 18, 18, 15, 5, 14, 5, 3, 6, 18, 7, 5, 11, 6, 3, NA, 3, 11, 8, 10, 3, 7, 7, 12, 3, 5, 8, 4, 9, 15, 9, 3, 16, 3, 3, 11, 3, 11, 8, 14, 21, 21, 17, 9, 3, 18, 15, 6, 6, 12, 9, 15, 5, 13, 7, 6, 7, 9, 11, 21, 12, 7, 12, 6, 21, 15, 21, 16, 12, 7, 18, 12, 20, 3, 10), after= c(15, 3, 6, 18, 14, 15, 6, NA, 6, 3, 18, NA, 3, 15, NA, 15, 11, NA, 10, 9, NA, 6, 6, 12, 3, NA, NA, 11, 9, 15, 21, 21, 6, 15, 9, 16, 9, 11, 14, 13, 5, NA, 3, 10, NA, 3, 18, 12, NA, NA, 8, 11, 14, NA, 13, NA, NA, 10, 6, 5, 15, 11, 12, NA, 5, NA, 15, 21, 11, NA, 3, NA, 12, NA, 17, 15)), class = c("rowwise_df", "tbl_df", "tbl", "data.frame"), row names = c(NA, -76L), groups = structure(list(.rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), row names = c(NA, -76L), class = c("tbl_df", "tbl", "data frame")))
英文:

I can't understand why the type of the variable condition does not change with this code

  1. df$condition &lt;- as.character(df)
  2. df[&quot;condition&quot;][df[&quot;condition&quot;] == &quot;1&quot;] &lt;- &quot;passive&quot;
  3. df[&quot;condition&quot;][df[&quot;condition&quot;] == &quot;2&quot;] &lt;- &quot;active&quot;

I get this error message

  1. ! Assigned data `as.character(df)` must be compatible with existing data.
  2. x Existing data has 76 rows.
  3. x Assigned data has 3 rows.
  4. i Only vectors of size 1 are recycled.
  5. Backtrace:
  6. 1. base::`$&lt;-`(`*tmp*`, condition, value = `&lt;chr&gt;`)
  7. 12. tibble (local) `&lt;fn&gt;`(`&lt;vctrs___&gt;`)

My data

  1. structure(list(condition = c(2, 2, 1, 2, 2, 1, 1, 2, 2, 2, 1,
  2. 2, 2, 1, 2, 1, 2, 1, 1, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 1, 2, 1,
  3. 2, 1, 1, 2, 1, 1, 2, 2, 1, 2, 1, 1, 1, 2, 2, 1, 2, 1, 1, 2, 2,
  4. 1, 2, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 1,
  5. 2, 1), before= c(13, 7, 18, 18, 15, 5, 14, 5, 3, 6,
  6. 18, 7, 5, 11, 6, 3, NA, 3, 11, 8, 10, 3, 7, 7, 12, 3, 5, 8, 4,
  7. 9, 15, 9, 3, 16, 3, 3, 11, 3, 11, 8, 14, 21, 21, 17, 9, 3, 18,
  8. 15, 6, 6, 12, 9, 15, 5, 13, 7, 6, 7, 9, 11, 21, 12, 7, 12, 6,
  9. 21, 15, 21, 16, 12, 7, 18, 12, 20, 3, 10), after= c(15,
  10. 3, 6, 18, 14, 15, 6, NA, 6, 3, 18, NA, 3, 15, NA, 15, 11, NA,
  11. 10, 9, NA, 6, 6, 12, 3, NA, NA, 11, 9, 15, 21, 21, 6, 15, 9,
  12. 16, 9, 11, 14, 13, 5, NA, 3, 10, NA, 3, 18, 12, NA, NA, 8, 11,
  13. 14, NA, 13, NA, NA, 10, 6, 5, 15, 11, 12, NA, 5, NA, 15, 21,
  14. 11, NA, 3, NA, 12, NA, 17, 15)), class = c(&quot;rowwise_df&quot;, &quot;tbl_df&quot;,
  15. &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -76L), groups = structure(list(
  16. .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
  17. 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
  18. 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
  19. 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L,
  20. 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L,
  21. 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L,
  22. 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L,
  23. 76L), ptype = integer(0), class = c(&quot;vctrs_list_of&quot;,
  24. &quot;vctrs_vctr&quot;, &quot;list&quot;))), row.names = c(NA, -76L), class = c(&quot;tbl_df&quot;,
  25. &quot;tbl&quot;, &quot;data.frame&quot;)))

答案1

得分: 1

  1. 由于条件方便地为12,您可以执行以下操作:
  2. df$condition <- c('active', 'passive')[df$condition]
  3. # # A tibble: 76 × 3
  4. # # Rowwise:
  5. # condition before after
  6. # <chr> <dbl> <dbl>
  7. # 1 passive 13 15
  8. # 2 passive 7 3
  9. # 3 active 18 6
  10. # 4 passive 18 18
  11. # 5 passive 15 14
  12. # 6 active 5 15
  13. # 7 active 14 6
  14. # 8 passive 5 NA
  15. # 9 passive 3 6
  16. # 10 passive 6 3
  17. # # … with 66 more rows
  18. # ℹ 使用 `print(n = ...)` 查看更多行
英文:

Since condition is conveniently 1 or 2, you can do:

  1. df$condition &lt;- c(&#39;active&#39;, &#39;passive&#39;)[df$condition]
  2. # # A tibble: 76 &#215; 3
  3. # # Rowwise:
  4. # condition before after
  5. # &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
  6. # 1 passive 13 15
  7. # 2 passive 7 3
  8. # 3 active 18 6
  9. # 4 passive 18 18
  10. # 5 passive 15 14
  11. # 6 active 5 15
  12. # 7 active 14 6
  13. # 8 passive 5 NA
  14. # 9 passive 3 6
  15. # 10 passive 6 3
  16. # # … with 66 more rows
  17. # # ℹ Use `print(n = ...)` to see more rows

答案2

得分: 0

df$condition <- factor(df$condition, levels = c(1, 2), labels = c("passive", "active"))

或者使用 tidyverse / dyplr

df %>% mutate(condition = factor(condition, c(1, 2), c("passive", "active")))

英文:

I suggest not to change your numeric to characters, but directly make a factor of it like this

  1. df$condition &lt;- factor(df$condition, levels = c(1, 2), labels = c(&quot;passive&quot;, &quot;active&quot;))

Or using tidyverse / dyplr

  1. df %&gt;%
  2. mutate(condition = factor(condition, c(1, 2), c(&quot;passive&quot;, &quot;active&quot;)))
  3. # A tibble: 76 &#215; 3
  4. # Rowwise:
  5. condition before after
  6. &lt;fct&gt; &lt;dbl&gt; &lt;dbl&gt;
  7. 1 active 13 15
  8. 2 active 7 3
  9. 3 passive 18 6
  10. 4 active 18 18
  11. 5 active 15 14
  12. 6 passive 5 15
  13. 7 passive 14 6
  14. 8 active 5 NA
  15. 9 active 3 6
  16. 10 active 6 3
  17. # … with 66 more rows
  18. # ℹ Use `print(n = ...)` to see more rows

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

发表评论

匿名网友

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

确定