[R] Your data set manipulations

Rolf Turner ro||turner @end|ng |rom po@teo@net
Mon Sep 16 11:05:09 CEST 2024


On Mon, 16 Sep 2024 09:28:14 +0200
Francesca <francesca.pancotto using 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



More information about the R-help mailing list