Re: [R] how to turn column into column names and fill it with values

2020-09-29 Thread Bert Gunter
A simpler, cleaner, and maybe faster approach is to use outer(): nm <- unique(dat$PLATE) dat <- cbind(dat, 1+outer(dat$PLATE,nm, "==")) names(dat)[-(1:3)] <- nm Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka

Re: [R] how to turn column into column names and fill it with values

2020-09-29 Thread Rui Barradas
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",

Re: [R] how to turn column into column names and fill it with values

2020-09-29 Thread Rui Barradas
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) %>%

Re: [R] how to turn column into column names and fill it with values

2020-09-29 Thread Bert Gunter
You should be able to figure this out yourself! The number of new columns you add **must agree with the number of unique id's PLATE** ! Here is the general approach, slightly more efficient, probably: PL = dat$PLATE nm<- unique(PL) len <- length(nm) m <- matrix(0, ncol = len, nrow = nrow(dat),

Re: [R] how to turn column into column names and fill it with values

2020-09-29 Thread Ana Marija
oh it seems that I can just use your last line of code and solve my problem: m2=tapply(mc$IID, list(FID=mc$FID, PLATE=mc$PLATE), mean) m2=as.data.frame(m2) library(data.table) m3=setDT(m2, keep.rownames = TRUE)[] colnames(m3)[1] <- "FID" mt=merge(mc,m3,by="FID" for(i in 4:ncol(mt)) mt[,i] <- 1 +

Re: [R] how to turn column into column names and fill it with values

2020-09-29 Thread Ana Marija
HI Bert, thank you for getting back to me. I tried this: > dat <- cbind(mc, matrix(0,ncol = 34)) > head(dat) FID IID PLATE 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 1 fam0110 G110 4RWG569 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 fam0113 G113 cherry 0 0

Re: [R] how to turn column into column names and fill it with values

2020-09-29 Thread Bert Gunter
I am not sure reshape2 is appropriate for this task, but, assuming I understand correctly, it's quite easy without it. The following is one way, which probably can be done more elegantly and efficiently, but I think it does what you want. "dat" is your example data frame, in which the columns

[R] how to turn column into column names and fill it with values

2020-09-29 Thread Ana Marija
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] 16254 > length(unique(mc$PLATE)) [1] 34 I am trying to make a