Dear all, I have a data set where I want to make a matrix of the mean values using 3rd column (value). Where rows are unique “loc” and columns would be “variables”. If there is one observation or no observation just use “NA”.> dput(d1)structure(list(loc = structure(c(6L, 9L, 9L, 9L, 9L, 4L, 4L, 4L, 4L, 4L, 4L, 13L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 1L, 3L, 3L, 3L, 2L), .Label = c("AX09_126", "AX09_628", "AX09_924", "IALA08_3299", "JA08_246", "JA08_3299", "JA08_408", "JE08_246", "JE08_3299", "JE08_408", "NO08_246", "NO08_408", "PP08_3299"), class "factor"), variables = structure(c(5L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 1L, 5L, 5L, 5L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 3L, 7L, 4L, 4L, 4L, 7L, 7L, 7L, 7L ), .Label = c("i", "r", "s", "t", "x", "y", "z"), class = "factor"), value = structure(c(1L, 21L, 2L, 1L, 30L, 32L, 4L, 32L, 5L, 20L, 5L, 1L, 24L, 12L, 26L, 24L, 12L, 15L, 26L, 14L, 15L, 23L, 13L, 11L, 23L, 25L, 23L, 6L, 7L, 3L, 8L, 19L, 3L, 19L, 28L, 16L, 18L, 18L, 17L, 31L, 29L, 10L, 27L, 22L, 9L, 22L, 22L, 9L, 9L, 35L, 33L, 33L, 33L, 34L), .Label = c("0", "0,3333", "142,1", "1506,00006", "1520,66664", "170,36", "18,6185", "19,2933", "20,2125", "20,7423", "225,94", "23,2245", "24,3526", "24,3685", "24,4593", "242,84", "25,7422", "26,34", "26,5989", "30,33336284", "36,33336608", "39,913", "41,4933", "43,9104", "47,1241", "49,3467", "49,5715", "50,0037", "50,7974", "53,6667", "56,2778", "76", "76,5", "77,6667", "82"), class = "factor")), .Names c("loc", "variables", "value"), class = "data.frame", row.names = c(NA, -54L)) Thanks Nico [[alternative HTML version deleted]]
Try: d1$value<-as.numeric(gsub(",","",as.character(d1$value))) library(reshape2) res <- dcast(d1,loc~variables,value.var="value",mean) A.K. ----- Original Message ----- From: Nico Met <nicomet80 at gmail.com> To: R help <r-help at r-project.org> Cc: Sent: Tuesday, October 8, 2013 10:47 AM Subject: [R] matrix of mean values Dear all, I have a data set where I want to make a matrix of the mean values using 3rd column (value). Where rows are unique ?loc? and columns would be ?variables?. If there is one observation or no observation just use ?NA?.> dput(d1)structure(list(loc = structure(c(6L, 9L, 9L, 9L, 9L, 4L, 4L, 4L, 4L, 4L, 4L, 13L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 1L, 3L, 3L, 3L, 2L), .Label = c("AX09_126", "AX09_628", "AX09_924", "IALA08_3299", "JA08_246", "JA08_3299", "JA08_408", "JE08_246", "JE08_3299", "JE08_408", "NO08_246", "NO08_408", "PP08_3299"), class "factor"), ? ? variables = structure(c(5L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, ? ? 7L, 7L, 7L, 7L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 3L, 3L, ? ? 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 1L, 5L, 5L, 5L, 6L, 6L, 7L, ? ? 7L, 7L, 7L, 7L, 7L, 7L, 3L, 7L, 4L, 4L, 4L, 7L, 7L, 7L, 7L ? ? ), .Label = c("i", "r", "s", "t", "x", "y", "z"), class = "factor"), ? ? value = structure(c(1L, 21L, 2L, 1L, 30L, 32L, 4L, 32L, 5L, ? ? 20L, 5L, 1L, 24L, 12L, 26L, 24L, 12L, 15L, 26L, 14L, 15L, ? ? 23L, 13L, 11L, 23L, 25L, 23L, 6L, 7L, 3L, 8L, 19L, 3L, 19L, ? ? 28L, 16L, 18L, 18L, 17L, 31L, 29L, 10L, 27L, 22L, 9L, 22L, ? ? 22L, 9L, 9L, 35L, 33L, 33L, 33L, 34L), .Label = c("0", "0,3333", ? ? "142,1", "1506,00006", "1520,66664", "170,36", "18,6185", ? ? "19,2933", "20,2125", "20,7423", "225,94", "23,2245", "24,3526", ? ? "24,3685", "24,4593", "242,84", "25,7422", "26,34", "26,5989", ? ? "30,33336284", "36,33336608", "39,913", "41,4933", "43,9104", ? ? "47,1241", "49,3467", "49,5715", "50,0037", "50,7974", "53,6667", ? ? "56,2778", "76", "76,5", "77,6667", "82"), class = "factor")), .Names c("loc", "variables", "value"), class = "data.frame", row.names = c(NA, -54L)) Thanks Nico ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.
To make it matrix: row.names(res)<- res[,1] ?res1<- as.matrix(res[,-1]) A.K. ----- Original Message ----- From: arun <smartpink111 at yahoo.com> To: Nico Met <nicomet80 at gmail.com> Cc: R help <r-help at r-project.org> Sent: Tuesday, October 8, 2013 10:54 AM Subject: Re: [R] matrix of mean values Try: d1$value<-as.numeric(gsub(",","",as.character(d1$value))) library(reshape2) res <- dcast(d1,loc~variables,value.var="value",mean) A.K. ----- Original Message ----- From: Nico Met <nicomet80 at gmail.com> To: R help <r-help at r-project.org> Cc: Sent: Tuesday, October 8, 2013 10:47 AM Subject: [R] matrix of mean values Dear all, I have a data set where I want to make a matrix of the mean values using 3rd column (value). Where rows are unique ?loc? and columns would be ?variables?. If there is one observation or no observation just use ?NA?.> dput(d1)structure(list(loc = structure(c(6L, 9L, 9L, 9L, 9L, 4L, 4L, 4L, 4L, 4L, 4L, 13L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 1L, 3L, 3L, 3L, 2L), .Label = c("AX09_126", "AX09_628", "AX09_924", "IALA08_3299", "JA08_246", "JA08_3299", "JA08_408", "JE08_246", "JE08_3299", "JE08_408", "NO08_246", "NO08_408", "PP08_3299"), class "factor"), ? ? variables = structure(c(5L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, ? ? 7L, 7L, 7L, 7L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 3L, 3L, ? ? 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 1L, 5L, 5L, 5L, 6L, 6L, 7L, ? ? 7L, 7L, 7L, 7L, 7L, 7L, 3L, 7L, 4L, 4L, 4L, 7L, 7L, 7L, 7L ? ? ), .Label = c("i", "r", "s", "t", "x", "y", "z"), class = "factor"), ? ? value = structure(c(1L, 21L, 2L, 1L, 30L, 32L, 4L, 32L, 5L, ? ? 20L, 5L, 1L, 24L, 12L, 26L, 24L, 12L, 15L, 26L, 14L, 15L, ? ? 23L, 13L, 11L, 23L, 25L, 23L, 6L, 7L, 3L, 8L, 19L, 3L, 19L, ? ? 28L, 16L, 18L, 18L, 17L, 31L, 29L, 10L, 27L, 22L, 9L, 22L, ? ? 22L, 9L, 9L, 35L, 33L, 33L, 33L, 34L), .Label = c("0", "0,3333", ? ? "142,1", "1506,00006", "1520,66664", "170,36", "18,6185", ? ? "19,2933", "20,2125", "20,7423", "225,94", "23,2245", "24,3526", ? ? "24,3685", "24,4593", "242,84", "25,7422", "26,34", "26,5989", ? ? "30,33336284", "36,33336608", "39,913", "41,4933", "43,9104", ? ? "47,1241", "49,3467", "49,5715", "50,0037", "50,7974", "53,6667", ? ? "56,2778", "76", "76,5", "77,6667", "82"), class = "factor")), .Names c("loc", "variables", "value"), class = "data.frame", row.names = c(NA, -54L)) Thanks Nico ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.
Hi Nico, Try: setAs("character", "num.commas", ??????? function(from) as.numeric(gsub(",", "", from) ) ) d1 <- read.csv("cont.txt",header=TRUE,sep="\t",colClasses=c("numeric","character","num.commas")) colnames(d1)[-3] <- c("loc","variables") library(reshape2) ?res <- dcast(d1,loc~variables,value.var="value",mean) row.names(res) <- res[,1] ?res1 <- as.matrix(res[,-1]) ?dim(res1) #[1] 129 190 A.K. On Tuesday, October 15, 2013 8:07 AM, Nico Met <nicomet80 at gmail.com> wrote: Hi Arun, many thanks. Here I attached the data. Regards Nico
Hi Nico, Try: dat<-read.table("pollShed.txt", header = T, sep = "\t",dec=",") ?as.vector(sapply(split(dat,dat$name1),nrow)) ?#[1]? 86? 10? 43?? 3? 20?? 7? 25? 12? 18?? 3?? 7? 12? 12? 14?? 3? 14? 25 175? 59 #[20]? 49? 16?? 3? 16? 14? 25? 14? 19?? 3? 18?? 7?? 4?? 3? 45? 14? 16?? 3? 12 238 #[39]? 12?? 3? 25?? 9? 20? 12? 12? 15? 20?? 7 170? 16? 12? 45? 14? 25?? 3? 12? 14 #[58] 397?? 1?? 3? 10 542? 14? 14? 12? 24? 12?? 3? 14? 20?? 3?? 4?? 3? 40? #### list element 59 produce the error lst1 <- split(dat,dat$name1) t.test(lst1[[59]][,2],dat[,2]) #Error in t.test.default(lst1[[59]][, 2], dat[, 2]) : #? not enough 'x' observations ?res <- sapply(lst1[sapply(lst1,nrow)>1],function(x) t.test(x[,2],dat[,2])$p.value) ?head(res) #????? -99999?????? 108946?????? 111364?????? 140400?????? 149067?????? 206262 #4.508590e-01 9.142790e-05 4.911925e-09 7.124666e-01 1.376976e-03 3.161232e-02 A.K. On Wednesday, October 23, 2013 8:27 AM, Nico Met <nicomet80 at gmail.com> wrote: Many thanks Arun for the suggestion.? However, I tried following and do not know why it shows error. Data attached. ?dat<-read.table("pollShed.txt", header = T, sep = "\t",dec=",") # works fine? ?mean1<-sapply(split(dat,dat$name1),function(x) t.test(x[,2],dat[,2])$statistic)? # Error in t.test.default(x[, 2], dat[, 2]) : not enough 'x' observations ?pval<-sapply(split(dat,dat$name1),function(x) t.test(x[,2],dat[,2])$p.value) # Error in t.test.default(x[, 2], dat[, 2]) : not enough 'x' observations Thanks in advance