英文:
Cannot change the variable type
问题
I can't understand why the type of the variable "condition" does not change with this code:
我不明白为什么变量"condition"的类型在这段代码中没有改变:
df$condition <- as.character(df)
df["condition"][df["condition"] == "1"] <- "passive"
df["condition"][df["condition"] == "2"] <- "active"
I get this error message:
我收到以下错误消息:
! Assigned data `as.character(df)` must be compatible with existing data.
x Existing data has 76 rows.
x Assigned data has 3 rows.
i Only vectors of size 1 are recycled.
Backtrace:
1. base::`$<-`(`*tmp*`, condition, value = `<chr>`)
12. tibble (local) `<fn>`(`<vctrs___>`)
My data:
我的数据:
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
df$condition <- as.character(df)
df["condition"][df["condition"] == "1"] <- "passive"
df["condition"][df["condition"] == "2"] <- "active"
I get this error message
! Assigned data `as.character(df)` must be compatible with existing data.
x Existing data has 76 rows.
x Assigned data has 3 rows.
i Only vectors of size 1 are recycled.
Backtrace:
1. base::`$<-`(`*tmp*`, condition, value = `<chr>`)
12. tibble (local) `<fn>`(`<vctrs___>`)
My data
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")))
答案1
得分: 1
由于条件方便地为1或2,您可以执行以下操作:
df$condition <- c('active', 'passive')[df$condition]
# # A tibble: 76 × 3
# # Rowwise:
# condition before after
# <chr> <dbl> <dbl>
# 1 passive 13 15
# 2 passive 7 3
# 3 active 18 6
# 4 passive 18 18
# 5 passive 15 14
# 6 active 5 15
# 7 active 14 6
# 8 passive 5 NA
# 9 passive 3 6
# 10 passive 6 3
# # … with 66 more rows
# ℹ 使用 `print(n = ...)` 查看更多行
英文:
Since condition is conveniently 1 or 2, you can do:
df$condition <- c('active', 'passive')[df$condition]
# # A tibble: 76 × 3
# # Rowwise:
# condition before after
# <chr> <dbl> <dbl>
# 1 passive 13 15
# 2 passive 7 3
# 3 active 18 6
# 4 passive 18 18
# 5 passive 15 14
# 6 active 5 15
# 7 active 14 6
# 8 passive 5 NA
# 9 passive 3 6
# 10 passive 6 3
# # … with 66 more rows
# # ℹ 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
df$condition <- factor(df$condition, levels = c(1, 2), labels = c("passive", "active"))
Or using tidyverse
/ dyplr
df %>%
mutate(condition = factor(condition, c(1, 2), c("passive", "active")))
# A tibble: 76 × 3
# Rowwise:
condition before after
<fct> <dbl> <dbl>
1 active 13 15
2 active 7 3
3 passive 18 6
4 active 18 18
5 active 15 14
6 passive 5 15
7 passive 14 6
8 active 5 NA
9 active 3 6
10 active 6 3
# … with 66 more rows
# ℹ Use `print(n = ...)` to see more rows
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论