You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am having some difficulties calculating rowMeans using c_across with labelled variables. The following code recreates two data frames I am using. The problem occurs with df1 and not with df, although the code is identical. But I think there is something about the way that value labels have been assigned to the variables in df1 that is causing the problem. Please note that using rowMeans() does work just fine; the problem appears to be the interaction of labelled variables in a particular way with c_across(). I really like the way that c_across() does not necessitate reattaching variables to the data.frame. In the olden times, I would have used mutate_at() to get at this.
Note: I often use car::Recode() rather than dplyr::Recode() because I find the syntax a little simpler, and because of path dependency; I have a ton of historic code that relies on it; I think dplyr::recode().
The code below returns the following error:
Error: Problem with mutate() input market_liberalism.
x labels must be unique.
Input market_liberalism is mean(c_across(market1:market2)).
The error occurred in row 1.
#Install car package if necessary#install.packages('car')
library(tidyverse)
library(car)
library(labelled)
#this recreates df1
structure(list(PESE15= structure(c(3, 5, 5, 8, NA), label="The Government Should Leave it Entirely to the Private Sector to Create Jobs", na_values= c(8, 9), format.spss="F1.0", display_width=0L, labels= c(`Strongly agree`=1, `Somewhat agree`=3, Somewhatdisagree=5, Stronglydisagree=7,D.K.=8, Refused=9), class= c("haven_labelled_spss", "haven_labelled", "vctrs_vctr", "double")), MBSA2= structure(c(3, 8, 1, 1, NA), label="People Who Do Not Get Ahead Should Blame Themselves Not the System", na_values=8, format.spss="F1.0", display_width=0L, labels= c(`Strongly agree`=1, Agree=2, Disagree=3, Stronglydisagree=4, `No opinion`=8), class= c("haven_labelled_spss", "haven_labelled", "vctrs_vctr", "double"))), row.names= c(NA, -5L), class= c("tbl_df", "tbl", "data.frame"), label="NSDstat generated file")->df1#use the car::Recode command to convert values to 0 to 1df1$market1<-car::Recode(df1$PESE15, "1=1; 3=0.75; 5=0.25; 7=0; 8=0.5; else=NA")
df1$market2<-car::Recode(df1$MBSA2, "1=1; 2=0.75; 3=0.25; 4=0; 8=0.5; else=NA")
#Use dplyr::c_across() to try to calculate the average df1 %>%
rowwise() %>%
mutate(market_liberalism=mean(
c_across(market1:market2)))
#Using RowMeans does work. df1 %>%
select(market1:market2) %>%
mutate(market_liberalism=rowMeans(., na.rm=T))
#that works, but then it is somewhat difficult to get it back into the original data.frame#setting value labels to NULL makes it work.
val_labels(df1$market1)<-NULL
val_labels(df1$market2)<-NULL#Try againdf1 %>%
rowwise() %>%
mutate(market_liberalism=mean(
c_across(market1:market2)))
#This makes df2, similar dataset
structure(list(cpsf6= structure(c(3, 7, 7, 1, 7, 7), label="The Government Should Leave it Entirely to the Private Sector to Create Jobs", na_values= c(8,
9), format.spss="F1.0", display_width=0L, labels= c(`Strongly Agree`=1,
`Somewhat Agree`=3, SomewhatDisagree=5, StronglyDisagree=7,
D.K.=8, Refused=9), class= c("haven_labelled_spss", "haven_labelled",
"vctrs_vctr", "double")), pese19= structure(c(3, 7, 3, 1, NA, 5), label="People Who Do Not Get Ahead Should Blame Themselves, Not the System", na_values= c(8, 9), format.spss="F1.0", display_width=0L, labels= c(`Strongly Agree`=1, `Somewhat Agree`=3, SomewhatDisagree=5, StronglyDisagree=7,
D.K.=8, Refused=9), class= c("haven_labelled_spss", "haven_labelled",
"vctrs_vctr", "double"))), row.names= c(NA, -6L), class= c("tbl_df", "data.frame"))->df2#use car::Recode() df2$market1<-car::Recode(df2$cpsf6, "1=1; 3=0.75; 5=0.25; 7=0; 8=0.5; else=NA", as.numeric=T)
df2$market2<-car::Recode(df2$pese19, "1=1; 3=0.75; 5=0.25; 7=0; 8=0.5; else=NA", as.numeric=T)
#df2 %>%
rowwise() %>%
mutate(market_liberalism=mean(
c_across(market1:market2)
, na.rm=T ))
Hi @sjkiss, there's a bug when combining two vectors with different labels for the same values.
This was fixed for labelled vectors but missed for labelled_spss.
@hadley this is the same bug fixed in ed2ddda, I'll chuck up a pull request.
I am having some difficulties calculating rowMeans using c_across with labelled variables. The following code recreates two data frames I am using. The problem occurs with
df1
and not withdf
, although the code is identical. But I think there is something about the way that value labels have been assigned to the variables indf1
that is causing the problem. Please note that usingrowMeans()
does work just fine; the problem appears to be the interaction of labelled variables in a particular way withc_across()
. I really like the way thatc_across()
does not necessitate reattaching variables to the data.frame. In the olden times, I would have usedmutate_at()
to get at this.Note: I often use
car::Recode()
rather thandplyr::Recode()
because I find the syntax a little simpler, and because of path dependency; I have a ton of historic code that relies on it; I thinkdplyr::recode()
.The code below returns the following error:
Results from
sessionInfo()
The text was updated successfully, but these errors were encountered: