I'm working on a data frame (dim: 10,155 by 33). Few rows & columns of the data frame are
rg[1:3, 1:4]
REF_NO children age_band status
1 2148 1 45-50 Partner
2 8099 1 61-65 Partner
3 6611 3 31-35 Partner
> table(rg_age_band)
18-21 22-25 26-30 31-35 36-40 41-45 45-50 51-55
63 456 927 1061 1134 1112 1359 1052
55-60 61-65 65-70 71+ Unknown
1047 881 598 410 55
For variable, age_band, I want to use tidyverse functions separate(), mutate() & chaining operator for following nested operations:
I'm using the following code:
library(tidyr); library(dplyr)
rg1=rg %>%
separate(age_band, into = c("a1", "a2"), sep="-") %>%
mutate(a1 = as.numeric(ifelse(rg$a1=="71+", 71, rg$a1)),
a2 = as.numeric(a2),
age = 0.5*(a1+a2)) %>%
select(-a1-a2)
Error: Column `a1` must be length 10155 (the number of rows) or one, not 0
Error: Column a1
must be length 10155 (the number of rows) or one, not 0 Please suggest what can be done. And when I run the code without '$' in ifelse statement, I get an error object 'a1' not found while usually, we don't need '$' while using chaining operator & mutate. Discussion on Similar question is not able to give any useful solution. I tried the peices of code and the problem is with
mutate(a1 = as.numeric(ifelse(rg$a1=="71+", 71, rg$a1))
also
#is producing warning
Expected 2 pieces. Missing pieces filled with `NA` in 465 rows```
EDIT: Attaching a sample data
The following code does not produce any errors:
rg <- data.frame(REF_NO = c(2148, 8099, 6611), children = c(1,1,3), age_band = c("45-50", "61-65", "71+"))
rg %>%
tidyr::separate(age_band, into = c("a1", "a2"), sep="-") %>%
mutate(a1 = as.numeric(ifelse(a1=="71+", 71, a1)),
a2 = as.numeric(a2),
age = 0.5*(a1+a2)) %>%
select(-a1, -a2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.