Skip to content
Back to formatted view

Raw Message

Message-ID: <fbecdfc0-71c5-6c58-50bd-53c493950115@sapo.pt>
Date: 2020-09-29T18:31:37Z
From: Rui Barradas
Subject: how to turn column into column names and fill it with values
In-Reply-To: <9e402ba4-ca29-5872-deb9-387dcf2a47f8@sapo.pt>

Hello,

Sorry, I didn't understand that 1 and 2 are the final values, I thought 
they would be counts of PLATE. I have changed the auxiliary column 
'counts' to 'flag'.


mc %>%
   group_by(PLATE) %>%
   mutate(flag = 2) %>%
   pivot_wider(
     id_cols = c("FID", "IID"),
     names_from = "PLATE",
     values_from = flag,
     values_fill = list(flag = 1)
   )


Hope this helps,

Rui Barradas

?s 19:19 de 29/09/20, Rui Barradas escreveu:
> Hello,
> 
> Something like this?
> 
> mc <- read.table(text = "
>  ????? FID? IID?? PLATE
> 1 fam0110 G110 4RWG569
> 2 fam0113 G113? cherry
> 3 fam0114 G114? cherry
> 4 fam0117 G117 4RWG569
> 5 fam0118 G118 5XAV049
> 6 fam0119 G119? cherry
> ", header = TRUE)
> 
> 
> 
> library(dplyr)
> library(tidyr)
> 
> mc %>%
>  ? group_by(PLATE) %>%
>  ? mutate(counts = n()) %>%
>  ? pivot_wider(
>  ??? id_cols = c("FID", "IID"),
>  ??? names_from = "PLATE",
>  ??? values_from = counts,
>  ??? values_fill = list(counts = 0)
>  ? )
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> ?s 17:18 de 29/09/20, Ana Marija escreveu:
>> Hello,
>>
>> I have a data frame like this:
>>
>>> head(mc)
>> ?????? FID? IID?? PLATE
>> 1 fam0110 G110 4RWG569
>> 2 fam0113 G113? cherry
>> 3 fam0114 G114? cherry
>> 4 fam0117 G117 4RWG569
>> 5 fam0118 G118 5XAV049
>> 6 fam0119 G119? cherry
>> ...
>>> dim(mc)
>> [1] 1625??? 4
>>> length(unique(mc$PLATE))
>> [1] 34
>>
>> I am trying to make a new data frame which would look like this:
>> ?????? FID? IID?? PLATE?? 4RWG569? cherry 5XAV049 ...
>> 1 fam0110 G110 4RWG569? 2? 1? 1
>> 2 fam0113 G113? cherry?? 1? 2? 1
>> 3 fam0114 G114? cherry?? 1? 2? 1
>> 4 fam0117 G117 4RWG569? 2? 1? 1
>> 5 fam0118 G118 5XAV049?? 2? 1? 1
>> 6 fam0119 G119? cherry?? 1? 2? 1
>> ...
>>
>> so the new data frame would have an additional 34 columns (for every
>> unique mc$PLATE) and if in the row of PLATE column the value is ==to
>> that column name I would have 2 otherwise 1
>>
>> I tried to do this with:
>>
>> library(reshape2)
>>> ? m2=dcast(mc, IID ~ PLATE)
>> Using PLATE as value column: use value.var to override.
>>
>> Please advise,
>> Ana
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.