0

I am trying to tweak some data to reduce the response range by changing 0 into 1 and anything larger than 4 into 4, to match a different data source, and calculate some stats accurately.

I've tried the options suggested by dplyr case_when throws error names' attribute [1] must be the same length as the vector [0] and the other question linked there, by keeping everything to numbers or NA - I want to analyse the NA outputs in the summarise clause later on. I have also tried NaN instead of NA_real, and deleting that line entirely. However, I am still getting errors.

X_YEAR is numeric, and the values in it are all integers or NA. It worked fine as an ifelse for just >=4 = 4, but I also need to combine 0 and 1 as 1.

library(tidyverse)
year_data <- data %>%
  mutate(YEAR = case_when(X_YEAR >= 4 ~ 4,
                                X_YEAR < 1 ~ 1,
                                is.na(X_YEAR) ~ NA_real_,
                                TRUE ~ X_YEAR))

Error in `mutate()`:
! Problem while computing `YEAR = case_when(...)`.
Caused by error in `` names(message) <- `*vtmp*` ``:
! 'names' attribute [1] must be the same length as the vector [0]

Sample data:
X-YEAR = 0,1,1,3,2,4,6,0,5,3,4,1,5,1,0 <- what I have
YEAR   = 1,1,1,3,2,4,4,1,4,3,4,1,4,1,1 <- what I want
Pendragon
  • 119
  • 9
  • Your code works with no error for me, considering that data is a dataframe. Just pay attention that in your example you have "X-YEAR" and in your code "X_YEAR" – Basti Oct 11 '22 at 14:22

0 Answers0