Dear Francesca,

As Rolf already pointed out, please provide more comprehensible information.

As shot into the blue, excluding the NAs when calculating the mean might help:


> mean(c(3, 5, NA))

[1] NA

> mean(c(3, 5, NA), na.rm = TRUE)
[1] 4

Cheers,
Thomas
________________________________
Von: R-help <r-help-boun...@r-project.org> im Auftrag von Rolf Turner 
<rolftur...@posteo.net>
Gesendet: Montag, 16. September 2024 11:05
An: Francesca <francesca.panco...@gmail.com>
Cc: R help <r-help@r-project.org>
Betreff: Re: [R] Your data set manipulations

On Mon, 16 Sep 2024 09:28:14 +0200
Francesca <francesca.panco...@gmail.com> wrote:

> Dear Contributors,
> I hope someone has found a similar issue.

I hope *not*! 😊️

> I have this data set,

You may have, but we haven't.  The data you provided have an
incomprehensible (to me at least) structure.  Please use dput()
to include your data in the message.

> cp1
> cp2
> role
> groupid
> 1
> 10
> 13
> 4
> 5
> 2
> 5
> 10

<SNIP>

<SNIP>

> 10
> 9
> 2
> 6
>
>
>
> I need to to average of groups, using the values of column groupid,
> and create a twin dataset in which the mean of the group is replaced
> instead of individual values.
> So for example, groupid 3, I calculate the mean (12+18)/2 and then I
> replace in the new dataframe, but in the same positions, instead of
> 12 and 18, the values of the corresponding mean.
> I found this solution, where db10_means is the output dataset, db10
> is my initial data.
>
> db10_means<-db10 %>%
>   group_by(groupid) %>%
>   mutate(across(starts_with("cp"), list(mean = mean)))

What does "%>%" mean?

> It works perfectly, except that for NA values,

I see no sign of there being any NAs in your data set.

> where it replaces to
> all group members the NA, while in some cases, the group is made of
> some NA and some values.
> So, when I have a group of two values and one NA, I would like that
> for those with a value, the mean is replaced, for those with NA, the
> NA is replaced.
> Here the mean function has not the na.rm=T option associated, but it
> appears that this solution cannot be implemented in this case. I am
> not even sure that this would be enough to solve my problem.
> Thanks for any help provided.

A more coherent message is required before I (at least) could possibly
give any help.

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Stats. Dep't. (secretaries) phone:
         +64-9-373-7599 ext. 89622
Home phone: +64-9-480-4619

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to