Re: [R] Using R to Calculate Coefficients of Relatedness and Kinship for Family Members
Hi, I don't quite understand the logic since some later assignment will overwrite the previous one. I simply assign for the current index and ignore the index after it. In addition, the column Dads.Renamed is not in the data.frame you defined in the post and I used Father_ID to substitute for it. Here is an implementation. Write back if this is not what you expect. pedigree.data Subject Twin_Stat Zygosity Father_ID Mother_ID 1 100307 TwinNotMZ 81352 51488 2 100408 Twin MZ 81594 51730 3 101006 Twin MZ 81149 51283 4 101107 NotTwin NotTwin 81833 51969 5 101309 NotTwin NotTwin 82248 52385 6 101410 TwinNotMZ 82061 52198 7 101915 NotTwin NotTwin 81841 51977 8 102008 NotTwin NotTwin 81882 52018 9 102311 Twin MZ 81543 51679 10 102816 Twin MZ 81283 51418 assignKinScore function(Input) { result - rep(0,dim(Input)[1] - 1) for (i in 1:(dim(Input)[1] - 1)) { if (as.character(Input$Twin_Stat[i]) == Twin as.character(Input$Twin_Stat[i + 1]) == Twin) { if (as.character(Input$Zygosity[i]) == MZ as.character(Input$Zygosity[i + 1]) == MZ) { result[i] - 0.5 } else { if (as.character(Input$Zygosity[i]) == DZ as.character(Input$Zygosity[i + 1]) == DZ) { result[i] - 0.25 } } } else { if (as.character(Input$Twin_Stat[i])==NotTwin as.character(Input$Twin_Stat[i + 1])==NotTwin) { if (Input$Mother_ID[i] == Input$Mother_ID[i + 1] Input$Father_ID[i] == Input$Father_ID[i + 1]) { result[i] - 0.25 } else { if (Input$Mother_ID[i] == Input$Mother_ID[i + 1] Input$Father_ID[i] != Input$Father_ID[i + 1]) { result[i] - 0.125 } } } } } return(result) } assignKinScore(pedigree.data) [1] 0.0 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.5 - JS Huang -- View this message in context: http://r.789695.n4.nabble.com/Using-R-to-Calculate-Coefficients-of-Relatedness-and-Kinship-for-Family-Members-tp4704232p4704243.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Want to convert list with vectore of dis similar lengths to data frame
Hi, The following works by appending NA to make up the maximum length of the list. test4 $Row1 [1] a b c d e $Row2 [1] a b c d e f g $Row3 [1] h i j k l m n as.data.frame(t(sapply(1:length(test4), function(x){c(test4[[x]],rep(NA,max(sapply(1:length(test4),function(x)length(test4[[x]])))-length(test4[[x]])))}))) V1 V2 V3 V4 V5 V6 V7 1 a b c d e NA NA 2 a b c d efg 3 h i j k lmn -- View this message in context: http://r.789695.n4.nabble.com/Want-to-convert-list-with-vectore-of-dis-similar-lengths-to-data-frame-tp4704196p4704200.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] help on output my own function result in ddply
Hi, There is a lot of if-else statement. r syntax rejects them. Here is an example of if-else example. In r we can code *ifelse(x2,y - 1,ifelse(x1,y - 0.5, y - 0))* for the following. if (x 2) { y = 1 } else { if (x 1) { y = 0.5 } else { y = 0 } } - JS Huang -- View this message in context: http://r.789695.n4.nabble.com/help-on-output-my-own-function-result-in-ddply-tp4704238p4704240.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] problem with function that adds rows to dataframe based on conditional statement
Hi Curtis, Maybe you forgot to tell us how the function seq_len is defined. - JS Huang -- View this message in context: http://r.789695.n4.nabble.com/problem-with-function-that-adds-rows-to-dataframe-based-on-conditional-statement-tp4704228p4704237.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] sampling dataframe based upon number of record occurrences
Here is an implementation with function named getSample. Some modification to the data was made so that it can be read as a table. fitting.set IDbyYear SiteID Year 1 42.24 A-Airport 2006 2 42.24 A-Airport 2006 3 42.24 A-Airport 2006 4 42.24 A-Airport 2006 5 42.24 A-Airport 2006 6 42.24 A-Airport 2006 7 45.32 A-Bark.Corral.East 2008 8 45.32 A-Bark.Corral.East 2008 9 45.36 A-Bark.Corral.East 2009 1045.40 A-Bark.Corral.East 2010 1145.40 A-Bark.Corral.East 2010 getSample function(x) { sites - unique(x$SiteID) years - unique(x$Year) result - data.frame() x$ID - seq(1,nrow(x)) for (i in 1:length(sites)) { for (j in 1:length(years)) { if (nrow(x[as.character(x$SiteID)==as.character(sites[i]) x$Year==years[j],]) 3) { sampledID - sample(x[as.character(x$SiteID)==as.character(sites[i]) x$Year==years[j],]$ID,3,replace=FALSE) for (k in 1:length(sampledID)) { result - rbind(result,x[x$ID==sampledID[k],-4]) } } } } names(result) - c(IDbyYear,SiteID,Year) rownames(result) - NULL return(result) } getSample(fitting.set) IDbyYearSiteID Year 142.24 A-Airport 2006 242.24 A-Airport 2006 342.24 A-Airport 2006 -- View this message in context: http://r.789695.n4.nabble.com/sampling-dataframe-based-upon-number-of-record-occurrences-tp4704144p4704154.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] sampling dataframe based upon number of record occurrences
Since you indicated there are six more columns in the data.frame, getSample modified below to take care of it. getSample function(x) { sites - unique(x$SiteID) years - unique(x$Year) result - data.frame() x$ID - seq(1,nrow(x)) for (i in 1:length(sites)) { for (j in 1:length(years)) { if (nrow(x[as.character(x$SiteID)==as.character(sites[i]) x$Year==years[j],]) 3) { sampledID - sample(x[as.character(x$SiteID)==as.character(sites[i]) x$Year==years[j],]$ID,3,replace=FALSE) for (k in 1:length(sampledID)) { result - rbind(result,x[x$ID==sampledID[k],-ncol(x)]) } } } } names(result) - names(x)[-ncol(x)] rownames(result) - NULL return(result) } getSample(fitting.set) IDbyYearSiteID Year 142.24 A-Airport 2006 242.24 A-Airport 2006 342.24 A-Airport 2006 -- View this message in context: http://r.789695.n4.nabble.com/sampling-dataframe-based-upon-number-of-record-occurrences-tp4704144p4704155.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] remove repeated string in list
Hi, Here is one for preserving the first strings. as.numeric in the previous posting is not necessary. temp $set1 [1] a b d x $set2 [1] b c q m $set3 [1] b f e k q h sapply(1:length(temp),function(x){c - list(); for (j in 1:x){c - c(c,temp[[j]])}; temp[[x]][table(unlist(c))[temp[[x]]]==1]}) [[1]] [1] a b d x [[2]] [1] c q m [[3]] [1] f e k h -- View this message in context: http://r.789695.n4.nabble.com/remove-repeated-string-in-list-tp4704166p4704191.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] remove repeated string in list
Hi, To avoid hardcoded 1:3, here is some revision. temp $set1 [1] a b d x $set2 [1] b c q m $set3 [1] b f e k q h sapply(1:*length(temp)*,function(x){temp[[x]][as.numeric(table(unlist(temp))[temp[[x]]])==1]}) [[1]] [1] a d x [[2]] [1] c m [[3]] [1] f e k h -- View this message in context: http://r.789695.n4.nabble.com/remove-repeated-string-in-list-tp4704166p4704190.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] remove repeated string in list
Hi, Here is one for removing repeated strings. temp $set1 [1] a b d x $set2 [1] b c q m $set3 [1] b f e k q h sapply(1:3,function(x){temp[[x]][as.numeric(table(unlist(temp))[temp[[x]]])==1]}) [[1]] [1] a d x [[2]] [1] c m [[3]] [1] f e k h -- View this message in context: http://r.789695.n4.nabble.com/remove-repeated-string-in-list-tp4704166p4704189.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Sum upto every twelth cell in a column
Here is an implementation. (x - c(23,35,22,11,10,1,14,15,13,15,17,16,154,13,24,25,25,25,25,25,22,11,15,15)) [1] 23 35 22 11 10 1 14 15 13 15 17 16 154 13 24 25 25 25 25 25 22 11 15 15 (y - c(0,cumsum(x))) [1] 0 23 58 80 91 101 102 116 131 144 159 176 192 346 359 383 408 433 458 483 508 530 541 556 [25] 571 (y[seq(13,length(y),12)] - y[seq(1,length(y)-12,12)]) [1] 192 379 -- View this message in context: http://r.789695.n4.nabble.com/Sum-upto-every-twelth-cell-in-a-column-tp4704007p4704087.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Sorting data frame by prepared order
Here is an implementation. t - data.frame(x=c(1,1,1,1,1,2,2,2,2,2),y=c(a,a,a,b,b,a,a,b,b,b)) t x y 1 1 a 2 1 a 3 1 a 4 1 b 5 1 b 6 2 a 7 2 a 8 2 b 9 2 b 10 2 b assignSeq function(test) { temp - test[order(test$x),] InC - numeric(length(test)) inD - unique(test$x) countAll - 0 for (i in 1:length(inD)) { countA - 0 countB - 0 for (j in 1:dim(temp[temp$x==inD[i],])[1]) { countAll - countAll + 1 if (temp$y[countAll] == a) { InC[countAll] - 2*countA countA - countA + 1 } else { InC[countAll] - 2*countB + 1 countB - countB + 1 } } } temp$seq - InC return(temp) } d - assignSeq(t) d[order(d$x,d$seq),-3] x y 1 1 a 4 1 b 2 1 a 5 1 b 3 1 a 6 2 a 8 2 b 7 2 a 9 2 b 10 2 b -- View this message in context: http://r.789695.n4.nabble.com/Sorting-data-frame-by-prepared-order-tp4704038p4704092.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] evaluate logical expressions
Hi, I assume input y to wrapper - function(y) {function(x) {(y)}} is a function. In the statement to assign gfunc[[i]]- gsub((Gene)([[:digit:]]), x[\\2], func[[i]]) the mode of gsub((Gene)([[:digit:]]), x[\\2], func[[i]]) is character. Is this the issue? -- View this message in context: http://r.789695.n4.nabble.com/evaluate-logical-expressions-tp4703964p4703970.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] integrate with vector arguments
Hi, The following works. f2 function(z) { f1 - function(t) { z*t + z*t^2 } return(f1) } sapply(1:5,function(x)integrate(f2(x),0,1)$value) [1] 0.83 1.67 2.50 3.33 4.17 -- View this message in context: http://r.789695.n4.nabble.com/integrate-with-vector-arguments-tp4703906p4703925.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] covert entire dataset to numeric while persuing percentage values
The following data.frame x as one column named Percent. x Percent 1 10% 2 20% 3 30% as.numeric(substr(x$Percent,1,nchar(x$Percent)-1)) [1] 10 20 30 -- View this message in context: http://r.789695.n4.nabble.com/covert-entire-dataset-to-numeric-while-persuing-percentage-values-tp4703862p4703880.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] format selected columns in dataframe as character
Try as.character like the following shows. dfx - data.frame( + group = c(rep('A', 8), rep('B', 15), rep('C', 6)), + sex = sample(c(M, F), size = 29, replace = TRUE), + age = runif(n = 29, min = 18, max = 54)) dfx group sex age 1 A M 41.35554346 2 A F 47.73245469 3 A F 42.97870796 4 A M 52.51180396 5 A F 46.72228944 6 A M 48.64668630 7 A M 36.07894452 8 A M 26.96805121 9 B M 30.67208692 10 B M 45.09322672 11 B M 31.86601692 12 B F 53.28861780 13 B M 27.74271305 14 B F 52.05845066 15 B F 18.94612430 16 B M 48.66673378 17 B F 53.07004762 18 B F 48.15222416 19 B M 32.17737802 20 B M 37.02122907 21 B M 39.31442046 22 B M 27.90392578 23 B M 44.70562356 24 C F 53.43127126 25 C M 49.85362283 26 C M 40.40779822 27 C F 31.41189728 28 C M 47.49351314 29 C M 46.34333618 summary(dfx) group sex age A: 8 F:10 Min. :18.94612 B:15 M:19 1st Qu.:32.17738 C: 6 Median :44.70562 Mean :41.46947 3rd Qu.:48.64669 Max. :53.43127 dfx$group - *as.character*(dfx$group) summary(dfx) group sex age Length:29 F:10 Min. :18.94612 Class :character M:19 1st Qu.:32.17738 Mode :character Median :44.70562 Mean :41.46947 3rd Qu.:48.64669 Max. :53.43127 dfx group sex age 1 A M 41.35554346 2 A F 47.73245469 3 A F 42.97870796 4 A M 52.51180396 5 A F 46.72228944 6 A M 48.64668630 7 A M 36.07894452 8 A M 26.96805121 9 B M 30.67208692 10 B M 45.09322672 11 B M 31.86601692 12 B F 53.28861780 13 B M 27.74271305 14 B F 52.05845066 15 B F 18.94612430 16 B M 48.66673378 17 B F 53.07004762 18 B F 48.15222416 19 B M 32.17737802 20 B M 37.02122907 21 B M 39.31442046 22 B M 27.90392578 23 B M 44.70562356 24 C F 53.43127126 25 C M 49.85362283 26 C M 40.40779822 27 C F 31.41189728 28 C M 47.49351314 29 C M 46.34333618 dfx$sex - *as.character*(dfx$sex) summary(dfx) group sex age Length:29 Length:29 Min. :18.94612 Class :character Class :character 1st Qu.:32.17738 Mode :character Mode :character Median :44.70562 Mean :41.46947 3rd Qu.:48.64669 Max. :53.43127 class(dfx$group) [1] character dfx$group [1] A A A A A A A A B B B B B B B B B B B B B B B C [25] C C C C C class(dfx$sex) [1] character dfx$sex [1] M F F M F M M M M M M F M F F M F F M M M M M F [25] M M F M M -- View this message in context: http://r.789695.n4.nabble.com/format-selected-columns-in-dataframe-as-character-tp4703863p4703878.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How many digits are there in left of dot of 0.0001 ?
Hi, Some modification to work for both positive and negative number: nchar(format(*abs*(a),scientific=FALSE))-(trunc(log10(max(1,trunc(abs(a)+1) -1. -- View this message in context: http://r.789695.n4.nabble.com/How-many-digits-are-there-in-left-of-dot-of-0-0001-tp4703842p4703872.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How many digits are there in left of dot of 0.0001 ?
Hi, I assume you want to know the digit count to the left of decimal point. If this is the case, then you may use trunc(log10(max(1,trunc(abs(a)+1 for a numerical variable a. Count 0.12 as having one digit to the left of decimal point. trunc(log10(max(1,trunc(abs(-10.99)+1 [1] 6 trunc(log10(max(1,trunc(abs(0)+1 [1] 1 trunc(log10(max(1,trunc(abs(9.999)+1 [1] 1 trunc(log10(max(1,trunc(abs(19.999)+1 [1] 2 trunc(log10(max(1,trunc(abs(-1999.999)+1 [1] 4 -- View this message in context: http://r.789695.n4.nabble.com/How-many-digits-are-there-in-left-of-dot-of-0-0001-tp4703842p4703847.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Processing key_column, begin_date, end_date in R
Hi, Here is an implemenation: date key_column begin_dateend_date 1 123456 2013-01-01 2014-01-01 2 123456 2013-07-01 2014-07-01 3 789102 2012-03-01 2014-03-01 4 789102 2015-02-01 2016-02-01 5 789102 2015-02-06 2016-02-06 y - t(sapply(unique(date$key_column),function(x) c(x,min(as.character(date[date$key_column==x,begin_date])),max(as.character(date[date$key_column==x,end_date]) y [,1] [,2] [,3] [1,] 123456 2013-01-01 2014-07-01 [2,] 789102 2012-03-01 2016-02-06 colnames(y) NULL colnames(y) - c(key_column,begin_date,end_date) y key_column begin_date end_date [1,] 123456 2013-01-01 2014-07-01 [2,] 789102 2012-03-01 2016-02-06 -- View this message in context: http://r.789695.n4.nabble.com/Processing-key-column-begin-date-end-date-in-R-tp4703835p4703850.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Processing key_column, begin_date, end_date in R
Hi, It's not as easy as I originally thought. Here is a revision with the function beginEnd to get it done. date key_column begin_dateend_date 1 123456 2013-01-01 2014-01-01 2 123456 2013-07-01 2014-07-01 3 789102 2012-03-01 2014-03-01 4 789102 2015-02-01 2016-02-01 5 789102 2015-02-06 2016-02-06 beginEnd function() { date[order(date$key_column,date$begin_date),] key - numeric(0) begin - character(0) end - character(0) currentKey - as.numeric(date$key_column[1]) key - c(key, currentKey) currentBegin - as.character(date$begin_date[1]) begin - c(begin, currentBegin) currentEnd - as.character(date$end_date[1]) for (i in 2:length(date$key_column)) { if (currentKey == as.numeric(date$key_column[i])) { if (currentEnd = as.character(date$begin_date[i])) { currentEnd - max(currentEnd, as.character(date$end_date[i])) } else { end - c(end, currentEnd) currentKey - as.numeric(date$key_column[i]) key - c(key, currentKey) currentBegin - as.character(date$begin_date[i]) begin - c(begin, currentBegin) currentEnd - as.character(date$end_date[i]) } if (i == length(date$key_column)) { end - c(end, currentEnd) } } else { end - c(end, currentEnd) currentKey - as.numeric(date$key_column[i]) key - c(key, currentKey) currentBegin - as.character(date$begin_date[i]) begin - c(begin, currentBegin) currentEnd - as.character(date$end_date[i]) if (i == length(date$key_column)) { end - list(end, currentEnd) } } } result - cbind(key, begin, end) colnames(result) - c(key.column,begin.date,end.date) return(result) } beginEnd() key.column begin.date end.date [1,] 123456 2013-01-01 2014-07-01 [2,] 789102 2012-03-01 2014-03-01 [3,] 789102 2015-02-01 2016-02-06 -- View this message in context: http://r.789695.n4.nabble.com/Processing-key-column-begin-date-end-date-in-R-tp4703835p4703852.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How many digits are there in left of dot of 0.0001 ?
Hi, To get the number of digits to the right of decimal point: nchar(format(a,scientific=FALSE))-(trunc(log10(max(1,trunc(abs(a)+1) -1. The part (trunc(log10(max(1,trunc(abs(a)+1) is the number of digits to the left of decimal. At the end, subtract 1 for the decimal point. Negative number needs more work. options(digits=10) a - 0.0001 nchar(format(a,scientific=FALSE))-(trunc(log10(max(1,trunc(abs(a)+1) -1 [1] 4 a - 999.123456 nchar(format(a,scientific=FALSE))-(trunc(log10(max(1,trunc(abs(a)+1) -1 [1] 6 -- View this message in context: http://r.789695.n4.nabble.com/How-many-digits-are-there-in-left-of-dot-of-0-0001-tp4703842p4703849.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Replace the value with 1 and 0
Hi, Here is an implementation. More data are added. An extra column hasRain is added instead of replacing column Amount. rain Year Month Day Amount 1 1950 1 10.0 2 1950 1 2 35.5 3 1950 1 3 17.8 4 1950 1 4 24.5 5 1950 1 5 12.3 6 1950 1 6 11.5 7 1950 1 75.7 8 1950 1 8 13.2 9 1950 1 9 11.3 10 1950 1 10 14.7 11 1950 1 11 11.9 12 1950 1 12 17.5 13 1950 1 138.1 14 1950 1 140.4 15 1950 1 150.0 16 1950 1 16 19.5 17 1950 1 17 10.7 18 1950 1 180.5 19 1950 1 19 12.7 20 1950 1 206.3 21 1950 2 10.0 22 1950 2 2 35.5 23 1950 2 3 17.8 24 1950 2 4 24.5 25 1950 2 5 12.3 26 1950 2 6 11.5 27 1950 2 75.7 28 1950 2 8 13.2 29 1950 2 9 11.3 30 1950 2 10 14.7 31 1950 2 11 11.9 32 1950 2 12 17.5 33 1950 2 138.1 34 1950 2 140.4 35 1950 2 150.0 36 1950 2 16 19.5 37 1950 2 17 10.7 38 1950 2 180.0 39 1950 2 190.0 40 1950 2 200.0 rain$hasRain - ifelse(rain$Amount0,1,0) rain Year Month Day Amount hasRain 1 1950 1 10.0 0 2 1950 1 2 35.5 1 3 1950 1 3 17.8 1 4 1950 1 4 24.5 1 5 1950 1 5 12.3 1 6 1950 1 6 11.5 1 7 1950 1 75.7 1 8 1950 1 8 13.2 1 9 1950 1 9 11.3 1 10 1950 1 10 14.7 1 11 1950 1 11 11.9 1 12 1950 1 12 17.5 1 13 1950 1 138.1 1 14 1950 1 140.4 1 15 1950 1 150.0 0 16 1950 1 16 19.5 1 17 1950 1 17 10.7 1 18 1950 1 180.5 1 19 1950 1 19 12.7 1 20 1950 1 206.3 1 21 1950 2 10.0 0 22 1950 2 2 35.5 1 23 1950 2 3 17.8 1 24 1950 2 4 24.5 1 25 1950 2 5 12.3 1 26 1950 2 6 11.5 1 27 1950 2 75.7 1 28 1950 2 8 13.2 1 29 1950 2 9 11.3 1 30 1950 2 10 14.7 1 31 1950 2 11 11.9 1 32 1950 2 12 17.5 1 33 1950 2 138.1 1 34 1950 2 140.4 1 35 1950 2 150.0 0 36 1950 2 16 19.5 1 37 1950 2 17 10.7 1 38 1950 2 180.0 0 39 1950 2 190.0 0 40 1950 2 200.0 0 tapply(rain$hasRain,list(rain$Year,rain$Month),sum) 1 2 1950 18 15 -- View this message in context: http://r.789695.n4.nabble.com/Replace-the-value-with-1-and-0-tp4703838p4703840.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Simple Histogram
Hi, Your data may look like the following and named speed.txt in working directory. Then the cod follows the data. The graph is attached as speed.pdf. Speed 50 52 55 57 58 59 60 61 62 63 64 65 65 65 67 68 68 68 68 69 69 70 71 72 72 72 73 73 73 73 75 76 77 78 79 speed - read.table(speed.txt,header=TRUE) speed Speed 1 50 2 52 3 55 4 57 5 58 6 59 7 60 8 61 9 62 1063 1164 1265 1365 1465 1567 1668 1768 1868 1968 2069 2169 2270 2371 2472 2572 2672 2773 2873 2973 3073 3175 3276 3377 3478 3579 hist(speed$Speed) Speed.pdf http://r.789695.n4.nabble.com/file/n4703616/Speed.pdf -- View this message in context: http://r.789695.n4.nabble.com/Simple-Histogram-tp4703615p4703616.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] irregular sequence of events
Hi, Herr is one implementation with function named eventList. start [1] 5 13 21 start [1] 5 13 21 end [1] 10 16 27 x [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 eventList function(start, end, x) { result - character(0) for (i in 1:length(start)) { if (i == 1) { if (start[i] 1) { result - c(result, rep(NA, start[i] - 1)) } result - c(result, rep(paste0(A,i), end[i] - start[i] + 1)) } else { if (start[i] end[i - 1] + 1) { result - c(result, rep(NA, start[i] - end[i - 1] - 1)) } result - c(result, rep(paste0(A, i), end[i] - start[i] + 1)) } } if (end[length(start)] length(x)) { result - c(result, rep(NA, length(x) - end[length(start)])) } return(result) } eventList(start, end, x) [1] NA NA NA NA A1 A1 A1 A1 A1 A1 NA NA A2 A2 A2 A2 NA NA NA [20] NA A3 A3 A3 A3 A3 A3 A3 NA NA NA -- View this message in context: http://r.789695.n4.nabble.com/irregular-sequence-of-events-tp4703579p4703624.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How do I access a specific element of a multi-dimensional list
Hi, Jim's answer is neat. There is an issue on the result. All are characters even though some are numeric or logic. The following implementation retains the variable type. x [[1]] [1] 2 3 5 [[2]] [1] aa bb cc [[3]] [1] TRUE FALSE TRUE getFirst function(aList) { result - list() for (i in 1:length(aList)) { result - c(result, aList[[i]][1]) } return(result) } getFirst(x) [[1]] [1] 2 [[2]] [1] aa [[3]] [1] TRUE -- View this message in context: http://r.789695.n4.nabble.com/How-do-I-access-a-specific-element-of-a-multi-dimensional-list-tp4703596p4703622.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] About Read in .csv Files with Chinese Characters
Hi Nicholas, I am not sure how much the following link can help but at least it is related to your question. http://r.789695.n4.nabble.com/Reading-Chinese-Language-GB2312-Input-td4647581.html Good luck! JS -- View this message in context: http://r.789695.n4.nabble.com/About-Read-in-csv-Files-with-Chinese-Characters-tp4703514p4703538.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] About Read in .csv Files with Chinese Characters
Hi, I tried your file in my Windows7 pc. It worked fine except the display of the variable name. R version 3.1.2 (2014-10-31) -- Pumpkin Helmet Copyright (C) 2014 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 (64-bit) Hello - read.csv(Hello.csv);Hello gender brand Ãû.Ö 1 m 1 Tom 2 f 2 David 3 f 2 Smith 4 fNA Nich 5 f 3 Dick 6 m 1 Shimm 7 m 1 Sam 8 f 4 Sandy 9 m 2 Elvin 10 f 3 Arlin -- View this message in context: http://r.789695.n4.nabble.com/About-Read-in-csv-Files-with-Chinese-Characters-tp4703514p4703518.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Averaging column scores when participants vary in number of observations
Hi, Another implication: data1 Observation Participant.ID Video.Coder Score 1 A 1 Donald 4 2 B 1 Tracy 5 3 C 2 Donald 6 4 D 3 Sam 2 5 E 3 Tracy 3 6 F 4 Donald 2 7 G 4 Tracy 1 8 H 5 Sam 8 tapply(data1$Score,data1$Participant.ID,mean) 1 2 3 4 5 4.5 6.0 2.5 1.5 8.0 -- View this message in context: http://r.789695.n4.nabble.com/Re-Averaging-column-scores-when-participants-vary-in-number-of-observations-tp4703549p4703561.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Numerical stability of eigenvalue and hessian matrix in R
Hi, Since all entries in your hessian matrix and grad vector are integers, I suggest you execute the following for mat assignment. mat - round(h_x(x),digits=0)*round(hess.h,digits=0) - round(grad(h_x, x),digits=0) %o% round(grad(h_x, x),digits=0) mat [,1] [,2] [,3] [,4] [1,]000 -4080400 [2,]0 7920 -160 [3,]00 121604000 [4,] -4080400 -1600 -792 -- View this message in context: http://r.789695.n4.nabble.com/Numerical-stability-of-eigenvalue-and-hessian-matrix-in-R-tp4703443p4703456.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Substituting elements in vector
Hi, Here is an implementation. my.vector [1] A B C D E F G vec1 [1] p q r s t vec2 [1] x y z final - as.character(unlist(sapply(my.vector,function(x) if(x==B){vec1}else{if(x==E){vec2}else{x}}))) final [1] A p q r s t C D x y z F G -- View this message in context: http://r.789695.n4.nabble.com/Substituting-elements-in-vector-tp4703395p4703431.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error when I attempt to create a list or a data frame
Hi, It appears that you used left double quotation marks and right double quotation marks in your vector for characters. The first lst assignment is copied from your post and indicates issues. I retyped with doulbe quotation mark and went through fine with the second lst assignment. Unicode for double quotation mark is U+0022 and for left double quotation mark U+201C and right double quotation mark U+201D. Although they look similar but they are not the same in unicode. lst - list(c(1,2),TRUE,c(“a”,“b”,“c”)) Error: unexpected input in lst - list(c(1,2),TRUE,c(“ lst - list(c(1,2),TRUE,c(a,b,c)) lst [[1]] [1] 1 2 [[2]] [1] TRUE [[3]] [1] a b c -- View this message in context: http://r.789695.n4.nabble.com/Error-when-I-attempt-to-create-a-list-or-a-data-frame-tp4703251p4703433.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Re categorizing components of varaibles
Hi, Here I use the data.frame b as an example to show how it can be accomplished. The sixth column of b has values 6 through 996 by an increment of 10. The statement sapply(b[,6],function(x)if(x==6){0}else{if(x==996){2}else{1}}) assigns 0 to 6, 2 to 996 and 1 to the rest in column 6. In your case you may use sapply(b[,6],function(x)if(x==24){2}else{if(x==26){3}else{1}}) (Note: did you forget to assign a value to 25?) a - matrix(1:1000, ncol=10, byrow=TRUE) b - data.frame(a) b[,6] - sapply(b[,6],function(x)if(x==6){0}else{if(x==996){2}else{1}}) b - data.frame(a) b[,6] [1] 6 16 26 36 46 56 66 76 86 96 106 116 126 136 146 156 166 176 186 196 206 216 226 236 [25] 246 256 266 276 286 296 306 316 326 336 346 356 366 376 386 396 406 416 426 436 446 456 466 476 [49] 486 496 506 516 526 536 546 556 566 576 586 596 606 616 626 636 646 656 666 676 686 696 706 716 [73] 726 736 746 756 766 776 786 796 806 816 826 836 846 856 866 876 886 896 906 916 926 936 946 956 [97] 966 976 986 996 b[,6] - sapply(b[,6],function(x)if(x==6){0}else{if(x==996){2}else{1}}) b[,6] [1] 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [50] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [99] 1 2 -- View this message in context: http://r.789695.n4.nabble.com/Re-categorizing-components-of-varaibles-tp4703424p4703434.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] sd, mean with a frequency distribution matrix
Hi, Just learned another way to calculate sd for a frequency distribution matrix: p - matrix(c(10,3,20,4,30,5),ncol=2,byrow=TRUE) p [,1] [,2] [1,] 103 [2,] 204 [3,] 305 rep(p[,1],p[,2]) [1] 10 10 10 20 20 20 20 30 30 30 30 30 sd(rep(p[,1],p[,2])) [1] 8.348471 -- View this message in context: http://r.789695.n4.nabble.com/sd-mean-with-a-frequency-distribution-matrix-tp4703218p4703441.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] compute values by condition in DF by rownames
Hi, I like the aggregate version. Here is an implementation with sapply and apply: data X2 gbm_tcga lusc_tcga ucec_tcga_pub 1 gbm_tcga 1.0 0.14053719 -0.102847164 2 gbm_tcga 1.0 0.04413434 0.013568055 3 gbm_tcga 1.0 -0.20003971 0.038971817 4 gbm_tcga 1.0 0.14569916 0.009947045 5 lusc_tcga 0.140537191 1. 0.133080708 6 lusc_tcga 0.044134345 1. 0.062024713 7 lusc_tcga -0.200039712 1. -0.130239551 8 lusc_tcga 0.145699156 1. 0.041796670 9 ucec_tcga_pub -0.102847164 0.13308071 1.0 10 ucec_tcga_pub 0.013568055 0.06202471 1.0 11 ucec_tcga_pub 0.038971817 -0.13023955 1.0 12 ucec_tcga_pub 0.009947045 0.04179667 1.0 sapply(levels(data[,1]),function(x) apply(abs(data[as.character(data$X2)==as.character(x),-1])0.2,2,sum)) gbm_tcga lusc_tcga ucec_tcga_pub gbm_tcga 4 1 0 lusc_tcga1 4 0 ucec_tcga_pub0 0 4 -- View this message in context: http://r.789695.n4.nabble.com/compute-values-by-condition-in-DF-by-rownames-tp4703351p4703387.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] sd, mean with a frequency distribution matrix
Hi, For the second one, sqrt((sum(p[,1]^2*p[,2])-(sum(p[,1]*p[,2]))^2/sum(p[,2]))/(sum(p[,2])-1)), please refer to the following link for an example to explain how it works. http://www.lboro.ac.uk/media/wwwlboroacuk/content/mlsc/downloads/var_stand_deviat_group.pdf For the first one: sd(unlist(sapply(1:dim(p)[1],function(i)rep(p[i,1],p[i,2].\: sapply(1:dim(p)[1],function(i)rep(p[i,1],p[i,2])) to get all in the first column of the matrix repeated the number of times in the second column. After that, make the resulting list to become a vector so that it can be executed with sd function. Here is some illustrative example. p [,1] [,2] [1,] 103 [2,] 204 [3,] 305 sapply(1:dim(p)[1],function(i)rep(p[i,1],p[i,2])) [[1]] [1] 10 10 10 [[2]] [1] 20 20 20 20 [[3]] [1] 30 30 30 30 30 unlist(sapply(1:dim(p)[1],function(i)rep(p[i,1],p[i,2]))) [1] 10 10 10 20 20 20 20 30 30 30 30 30 sd(unlist(sapply(1:dim(p)[1],function(i)rep(p[i,1],p[i,2] [1] 8.348471 -- View this message in context: http://r.789695.n4.nabble.com/sd-mean-with-a-frequency-distribution-matrix-tp4703218p4703338.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] compute values by condition in DF by rownames
Hi, I hope that someone can provide a better way to implement it. This is my implementation. data X2 gbm_tcga lusc_tcga ucec_tcga_pub 1 gbm_tcga 1.0 0.14053719 -0.102847164 2 gbm_tcga 1.0 0.04413434 0.013568055 3 gbm_tcga 1.0 -0.20003971 0.038971817 4 gbm_tcga 1.0 0.14569916 0.009947045 5 lusc_tcga 0.140537191 1. 0.133080708 6 lusc_tcga 0.044134345 1. 0.062024713 7 lusc_tcga -0.200039712 1. -0.130239551 8 lusc_tcga 0.145699156 1. 0.041796670 9 ucec_tcga_pub -0.102847164 0.13308071 1.0 10 ucec_tcga_pub 0.013568055 0.06202471 1.0 11 ucec_tcga_pub 0.038971817 -0.13023955 1.0 12 ucec_tcga_pub 0.009947045 0.04179667 1.0 test function(x) { tempMatrix - matrix(0,nrow=3,ncol=3) lev - levels(x$X2) for (i in 1:length(lev)) { tempMatrix[i,1] = sum(ifelse(abs(x[x$X2==lev[i],2])0.2,1,0)) tempMatrix[i,2] = sum(ifelse(abs(x[x$X2==lev[i],3])0.2,1,0)) tempMatrix[i,3] = sum(ifelse(abs(x[x$X2==lev[i],4])0.2,1,0)) } result - data.frame(lev,tempMatrix) names(result) - c(X2,gbm_tcga,lusc_tcga,tcga_pub) return(result) } test(data) X2 gbm_tcga lusc_tcga tcga_pub 1 gbm_tcga4 10 2 lusc_tcga1 40 3 ucec_tcga_pub0 04 -- View this message in context: http://r.789695.n4.nabble.com/compute-values-by-condition-in-DF-by-rownames-tp4703351p4703374.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] split dataframe to several dataframes in R
quote author='R help mailing list-2' Hi All,I have a dataframe called 'means' as shown below:iris1.csv - iris iris2.csv - iris names - c(iris1.csv, iris2.csv) dat - mget(names) lst4 - lapply(dat, function(x) apply(x[,-5], 2, mean)) # Build the new data frame means - as.data.frame(do.call(rbind, lst4)) means$source - names(lst4) means # Sepal.Length Sepal.Width Petal.Length Petal.Width isv source # iris1.csv 5.843.0573333.7581.199333 0.333 iris1.csv # iris2.csv 5.843.0573333.7581.199333 0.333 iris2.csvQUESTION: How can I split 'means' such that there are two files (dataframes) on my workspace:datframe 1# Sepal.Length Sepal.Width Petal.Length Petal.Width isv # iris1.csv 5.843.0573333.7581.199333 0.333dataframe 2:# Sepal.Length Sepal.Width Petal.Length Petal.Width isv# iris2.csv 5.843.0573333.758 1.199333 0.333 Many thanks,Asong. [[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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. /quote Quoted from: http://r.789695.n4.nabble.com/split-dataframe-to-several-dataframes-in-R-tp4703372.html _ Sent from http://r.789695.n4.nabble.com __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Coverage probability for a Poisson parameter
Hi, In your function cover, lambda1 and lambda2 are used but not in the argument of the function. I suppose that you need to have lambda1 and lambda2 in the argument of the function cover, like function(lambda1, lambda2, n, significance.level). Give it a try. cover - function(lambda, n, significance.level) { s1 - rpois(1,lambda1) s2 - rpois(1,lambda2) theta - lambda2/(lambda1+lambda2) s - s1+s2 z - qnorm(1-0.05/2) k - z^2 pi - s2/s lower - (pi+(k/(2*s))-z*sqrt((pi*(1-pi)+(k/4*s))/s))/(1+k/s) upper - (pi+(k/(2*s))+z*sqrt((pi*(1-pi)+(k/4*s))/s))/(1+k/s) if (theta = lower theta = upper){1} else {0} } -- View this message in context: http://r.789695.n4.nabble.com/Coverage-probability-for-a-Poisson-parameter-tp4702535p4703230.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] sd, mean with a frequency distribution matrix
Or if you want to perform the calculation without using sd: sqrt((sum(p[,1]^2*p[,2])-(sum(p[,1]*p[,2]))^2/sum(p[,2]))/(sum(p[,2])-1)) -- View this message in context: http://r.789695.n4.nabble.com/sd-mean-with-a-frequency-distribution-matrix-tp4703218p4703231.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Coverage probability for a Poisson parameter
Hi, Given the function cover, it's very likely that you will get 0 for both s1 and s1 with small value of lambda1 and lambda2. In that case the sum s will be 0. With s being 0, you will have issue with the expression in pi - s2/s and root - ((s2/s)*(1-s2/s)+k/(4*s))^(1/2). You need to take care of the case that s is 0 before proceeding calculating pi and root. cover - function(theta, lambda1, lambda2, significance.level) { s1 - rpois(1,lambda1) s2 - rpois(1,lambda2) theta - lambda2/(lambda1+lambda2) s - s1+s2 z - qnorm(1-0.05/2) k - z^2 pi - s2/s root - ((s2/s)*(1-s2/s)+k/(4*s))^(1/2) low - (s2+k/2)/(s+k)-((z*sqrt(s))/(s+k))*root hig - (s2+k/2)/(s+k)+((z*sqrt(s))/(s+k))*root if (theta = low theta = hig){1} else {0} } -- View this message in context: http://r.789695.n4.nabble.com/Coverage-probability-for-a-Poisson-parameter-tp4702535p4703238.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Genrating Ordinal Responses in R
Hi, Here assume there are four elements in the ordinal set y and take a random sample of size 10 according to the cumulative distribution given, or probability distribution p below. y - c(levels = c(First, Second, Third, Fourth)) y levels1 levels2 levels3 levels4 First Second Third Fourth (x - c(1/9, 1/4, 3/5, 1)) [1] 0.111 0.250 0.600 1.000 p - c(x[1], x[2]-x[1], x[3]-x[2], x[4]-x[3]) p [1] 0.111 0.139 0.350 0.400 sample(y, 10, replace=TRUE, prob=p) levels2 levels4 levels4 levels3 levels1 levels3 levels4 levels2 levels4 levels3 Second Fourth Fourth Third First Third Fourth Second Fourth Third -- View this message in context: http://r.789695.n4.nabble.com/Genrating-Ordinal-Responses-in-R-tp4703159p4703244.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Caleberating weights
Hi, It's not clear what wizi is in the constraint Sum(wizi) = Z. If you can provide some data, it may make the problem easier to understand. -- View this message in context: http://r.789695.n4.nabble.com/Caleberating-weights-tp4703226p4703245.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Coverage probability for a Poisson parameter
Hi, Some suggestion about the arguments of the function defined below. Since theta is calculated with the value of lambda1 and lambda2, there is no need to include theta in the argument. Or, your function can be defined as function(lambda1, lambda2, significance.level) cover - function(theta, lambda1, lambda2, significance.level) { s1 - rpois(1,lambda1) s2 - rpois(1,lambda2) theta - lambda2/(lambda1+lambda2) s - s1+s2 z - qnorm(1-0.05/2) k - z^2 pi - s2/s root - ((s2/s)*(1-s2/s)+k/(4*s))^(1/2) low - (s2+k/2)/(s+k)-((z*sqrt(s))/(s+k))*root hig - (s2+k/2)/(s+k)+((z*sqrt(s))/(s+k))*root if (theta = low theta = hig){1} else {0} } -- View this message in context: http://r.789695.n4.nabble.com/Coverage-probability-for-a-Poisson-parameter-tp4702535p4703248.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] lme: Can not find groupData object in a function could this be a scoping problem?
Hi, Unless you defined SS somewhere before you execute data - data.frame(group=c(rep(Cont,SS),rep(Exp,SS)), pre=pre,post=post), SS is not assigned. Maybe it is TS you intended? doit- function(TS,rho,premean,presd,RxEffect) { . . . # Prepare data frames for regression analyses. data - data.frame(group=c(rep(Cont,SS),rep(Exp,SS)), pre=pre,post=post) -- View this message in context: http://r.789695.n4.nabble.com/lme-Can-not-find-groupData-object-in-a-function-could-this-be-a-scoping-problem-tp4703243p4703247.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] sd, mean with a frequency distribution matrix
Hi, Try this. sd(unlist(sapply(1:dim(p)[1],function(i)rep(p[i,1],p[i,2] -- View this message in context: http://r.789695.n4.nabble.com/sd-mean-with-a-frequency-distribution-matrix-tp4703218p4703220.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] SIMPLE question
Hi, Maybe the following link helps. http://r.789695.n4.nabble.com/box-cox-td4363944.html -- View this message in context: http://r.789695.n4.nabble.com/SIMPLE-question-tp4703176p4703180.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] plotting this data
Hi, Here is an implementation. The image is uploaded as Rplot02.png. gen - read.table(geno.txt,header=TRUE) gen Genotype E1 E2E3 E4 E5 E6 E7 E8 E9 E10 E11 E12 E13 E14 E15 E16 E17 E18 1 G1 0.79 2.11 6.21 0.56 4.06 2.13 5.61 0.20 3.32 3.01 5.12 0.77 0.78 0.62 2.00 5.87 2.50 0.49 2 G2 0.59 0.92 10.11 0.74 4.01 1.12 4.58 0.70 2.61 1.49 4.59 0.20 1.33 0.62 2.67 5.58 2.22 0.93 3 G3 1.18 3.44 4.08 0.50 6.91 4.27 6.87 0.30 4.57 2.88 6.61 0.59 0.97 1.02 1.45 5.32 2.65 2.73 4 G4 2.17 4.46 6.51 0.35 4.80 5.99 5.44 1.20 4.21 3.59 4.89 0.39 2.26 2.05 3.33 8.55 2.10 2.39 5 G5 0.99 4.83 6.71 1.43 5.83 2.95 3.66 0.50 1.54 2.55 5.70 1.83 0.39 1.44 1.66 5.36 1.32 1.19 6 G6 1.57 3.94 7.49 0.61 5.88 2.74 7.22 1.19 4.81 3.72 5.59 0.38 1.36 1.43 3.94 9.15 2.50 2.56 7 G7 2.38 4.74 9.94 1.31 5.74 5.59 9.01 0.70 4.47 3.59 4.85 3.48 2.35 1.23 3.22 11.21 2.89 3.01 8 G8 0.98 2.56 12.02 3.31 5.41 2.03 5.92 0.30 4.39 4.19 6.10 0.39 1.97 0.62 4.93 8.89 3.45 2.95 E19 E20 E21 E22 E23 E24 1 0.42 0.73 5.66 4.61 3.98 0.32 2 0.40 0.68 4.28 3.89 2.98 1.78 3 1.31 2.63 6.51 5.42 5.43 0.60 4 0.03 1.81 6.05 6.31 6.53 1.94 5 0.08 1.03 5.38 2.45 5.18 0.54 6 0.08 0.68 6.46 6.02 5.28 2.52 7 0.33 0.91 9.15 7.03 6.39 2.13 8 0.01 0.62 8.35 5.71 5.86 0.38 x - expand.grid(gen[,1],names(gen)[2:dim(gen)[2]]) x Var1 Var2 1 G1 E1 2 G2 E1 3 G3 E1 4 G4 E1 5 G5 E1 6 G6 E1 7 G7 E1 8 G8 E1 9 G1 E2 10G2 E2 11G3 E2 12G4 E2 13G5 E2 14G6 E2 15G7 E2 16G8 E2 17G1 E3 18G2 E3 19G3 E3 20G4 E3 21G5 E3 22G6 E3 23G7 E3 24G8 E3 25G1 E4 26G2 E4 27G3 E4 28G4 E4 29G5 E4 30G6 E4 31G7 E4 32G8 E4 33G1 E5 34G2 E5 35G3 E5 36G4 E5 37G5 E5 38G6 E5 39G7 E5 40G8 E5 41G1 E6 42G2 E6 43G3 E6 44G4 E6 45G5 E6 46G6 E6 47G7 E6 48G8 E6 49G1 E7 50G2 E7 51G3 E7 52G4 E7 53G5 E7 54G6 E7 55G7 E7 56G8 E7 57G1 E8 58G2 E8 59G3 E8 60G4 E8 61G5 E8 62G6 E8 63G7 E8 64G8 E8 65G1 E9 66G2 E9 67G3 E9 68G4 E9 69G5 E9 70G6 E9 71G7 E9 72G8 E9 73G1 E10 74G2 E10 75G3 E10 76G4 E10 77G5 E10 78G6 E10 79G7 E10 80G8 E10 81G1 E11 82G2 E11 83G3 E11 84G4 E11 85G5 E11 86G6 E11 87G7 E11 88G8 E11 89G1 E12 90G2 E12 91G3 E12 92G4 E12 93G5 E12 94G6 E12 95G7 E12 96G8 E12 97G1 E13 98G2 E13 99G3 E13 100 G4 E13 101 G5 E13 102 G6 E13 103 G7 E13 104 G8 E13 105 G1 E14 106 G2 E14 107 G3 E14 108 G4 E14 109 G5 E14 110 G6 E14 111 G7 E14 112 G8 E14 113 G1 E15 114 G2 E15 115 G3 E15 116 G4 E15 117 G5 E15 118 G6 E15 119 G7 E15 120 G8 E15 121 G1 E16 122 G2 E16 123 G3 E16 124 G4 E16 125 G5 E16 126 G6 E16 127 G7 E16 128 G8 E16 129 G1 E17 130 G2 E17 131 G3 E17 132 G4 E17 133 G5 E17 134 G6 E17 135 G7 E17 136 G8 E17 137 G1 E18 138 G2 E18 139 G3 E18 140 G4 E18 141 G5 E18 142 G6 E18 143 G7 E18 144 G8 E18 145 G1 E19 146 G2 E19 147 G3 E19 148 G4 E19 149 G5 E19 150 G6 E19 151 G7 E19 152 G8 E19 153 G1 E20 154 G2 E20 155 G3 E20 156 G4 E20 157 G5 E20 158 G6 E20 159 G7 E20 160 G8 E20 161 G1 E21 162 G2 E21 163 G3 E21 164 G4 E21 165 G5 E21 166 G6 E21 167 G7 E21 168 G8 E21 169 G1 E22 170 G2 E22 171 G3 E22 172 G4 E22 173 G5 E22 174 G6 E22 175 G7 E22 176 G8 E22 177 G1 E23 178 G2 E23 179 G3 E23 180 G4 E23 181 G5 E23 182 G6 E23 183 G7 E23 184 G8 E23 185 G1 E24 186 G2 E24 187 G3 E24 188 G4 E24 189 G5 E24 190 G6 E24 191 G7 E24 192 G8 E24 names(gen) - NULL genfinal - data.frame(x, unlist(gen[,2:dim(gen)[2]])) names(genfinal) - c(Genotype,category,value) genfinal$category - as.numeric(genfinal$category) head(genfinal) Genotype category value 1 G11 0.79 2 G21 0.59 3 G31 1.18 4 G41 2.17 5 G51 0.99 6 G61 1.57 library(ggplot2) ggplot(genfinal,aes(x=category,y=value,colour=Genotype)) + geom_line() Rplot02.png http://r.789695.n4.nabble.com/file/n4703089/Rplot02.png -- View this message in context: http://r.789695.n4.nabble.com/plotting-this-data-tp4702926p4703089.html Sent from the R help mailing list archive at Nabble.com. __ 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
Re: [R] factor levels numeric values
Hi, Suppose your data frame is called data and the name of the factor column is named tobeConverted. I have tried this and it worked. Hope this helps. as.numeric(as.character(data$tobeConverted)) -- View this message in context: http://r.789695.n4.nabble.com/factor-levels-numeric-values-tp4699515p4703090.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] $ operator is invalid for atomic vectors
Hi, If x is a data frame, then x$getmean will try to get the vector named getmean in x. You put () after x$getmean. I think r is confused about it. It appears that you want to call a function named getmean(). -- View this message in context: http://r.789695.n4.nabble.com/operator-is-invalid-for-atomic-vectors-tp4703112p4703114.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Nonlinear integer programming question
Hi Rebecca, It will be very helpful if you can provide a set of specific functions g(x), h(x) and m(x). -- View this message in context: http://r.789695.n4.nabble.com/Nonlinear-integer-programming-question-tp4703122p4703128.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] function that calculates using preceding records
Hi, Here is an implementation: data - read.table(tree.txt,header=TRUE,sep=,,stringsAsFactors=FALSE) data treecode yearrw d 1 TC149 2014NA8 2 TC149 2013 0.080 NA 3 TC149 2012 0.125 NA 4 TC149 2011 0.120 NA 5 TC149 2010 0.125 NA 6 TC148 2014NA 34 7 TC148 2013 0.300 NA 8 TC148 2012 0.335 NA 9 TC148 2011 0.315 NA 10TC148 2010 0.455 NA 11TC147 2014NA 55.5 12TC147 2013 1.260 NA 13TC147 2012 1.115 NA 14TC147 2011 1.025 NA 15TC147 2010 1.495 NA 16TC146 2014NA 60 17TC146 2013 1.750 NA 18TC146 2012 1.810 NA 19TC146 2011 1.390 NA 20TC146 2010 1.940 NA calDiameter - function(x) + { + temp - x + for (i in 1:dim(temp)[1]) + { + if (dim(x[x$treecode==temp$treecode[i] x$year == temp$year[i] - 1 !is.na(x$rw),])[1] == 1 !is.na(temp$d[i])) + { + temp$d[i] - as.numeric(temp$d[i]) - as.numeric(x[x$treecode == temp$treecode[i] x$year == temp$year[i] - 1 !is.na(x$rw), ]$rw[1]) + } + } + return(temp) + } calDiameter(data) treecode yearrw d 1 TC149 2014NA 7.92 2 TC149 2013 0.080 NA 3 TC149 2012 0.125 NA 4 TC149 2011 0.120 NA 5 TC149 2010 0.125 NA 6 TC148 2014NA 33.7 7 TC148 2013 0.300 NA 8 TC148 2012 0.335 NA 9 TC148 2011 0.315 NA 10TC148 2010 0.455 NA 11TC147 2014NA 54.24 12TC147 2013 1.260 NA 13TC147 2012 1.115 NA 14TC147 2011 1.025 NA 15TC147 2010 1.495 NA 16TC146 2014NA 58.25 17TC146 2013 1.750 NA 18TC146 2012 1.810 NA 19TC146 2011 1.390 NA 20TC146 2010 1.940 NA There were 12 warnings (use warnings() to see them) -- View this message in context: http://r.789695.n4.nabble.com/function-that-calculates-using-preceding-records-tp4703024p4703069.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to solve this complex equation
Hi, The solution x to the equation is the parmater lambda = x/2 of a Poisson distribution with probability of 0.05 for the number of occurrence 2 or fewer. -- View this message in context: http://r.789695.n4.nabble.com/How-to-solve-this-complex-equation-tp4702997p4703070.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] assignment of categorical variables to matrix/table
Hi, Try this with three category variables. A [1] Baby Kid Teenager AdultMature B [1] Male Female C [1] PST MST CST EST expand.grid(Age=A, Sex=B, Zone=C) AgeSex Zone 1 Baby Male PST 2 Kid Male PST 3 Teenager Male PST 4 Adult Male PST 5Mature Male PST 6 Baby Female PST 7 Kid Female PST 8 Teenager Female PST 9 Adult Female PST 10 Mature Female PST 11 Baby Male MST 12 Kid Male MST 13 Teenager Male MST 14Adult Male MST 15 Mature Male MST 16 Baby Female MST 17 Kid Female MST 18 Teenager Female MST 19Adult Female MST 20 Mature Female MST 21 Baby Male CST 22 Kid Male CST 23 Teenager Male CST 24Adult Male CST 25 Mature Male CST 26 Baby Female CST 27 Kid Female CST 28 Teenager Female CST 29Adult Female CST 30 Mature Female CST 31 Baby Male EST 32 Kid Male EST 33 Teenager Male EST 34Adult Male EST 35 Mature Male EST 36 Baby Female EST 37 Kid Female EST 38 Teenager Female EST 39Adult Female EST 40 Mature Female EST -- View this message in context: http://r.789695.n4.nabble.com/assignment-of-categorical-variables-to-matrix-table-tp4702981p4703071.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Making a histogram
Hi, For your error object 't1971' not found can be corrected by subsetting. t2001 can be done similarly with 2001 replacing 1971. t1971 - data[year==1971,] t1971 VOT year Consonant 1 67 1971 k 2 127 1971 k 3 79 1971 k 4 150 1971 k 5 53 1971 k 6 65 1971 k 7 75 1971 k 8 109 1971 k 9 109 1971 t 10 126 1971 t 11 129 1971 t 12 119 1971 t 13 104 1971 t 14 153 1971 t 15 124 1971 t 16 107 1971 t 17 181 1971 t 18 166 1971 t -- View this message in context: http://r.789695.n4.nabble.com/Making-a-histogram-tp4702534p4702915.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Coverage probability for a Poisson parameter
Hi, After some thought, I found the treatment of sample mean equal 0 was not appropriate. I modified the function likelihood.ratio.test.Poisson. resulting.matrix now has 0.0512 as the average of type I error. function(lambda, sample.size, significance.level) { reject - 0 sample.mean - mean(rpois(sample.size, lambda)) if (sample.mean == 0) { test.statistics - 2 * sample.size * lambda } else { test.statistics - 2 * sample.size * (lambda - sample.mean + sample.mean * log(sample.mean / lambda)) } if (test.statistics = qchisq(1 - significance.level, 1)) {reject - 1} else {reject - 0} return(reject) } for (i in 1:500){ + resulting.matrix[i,1] - 0.01 * i + resulting.matrix[i,2] - mean(sapply(1:100,function(x) likelihood.ratio.test.Poisson(0.01*i,10,0.05))) + } mean(resulting.matrix[,2]) [1] 0.05102 -- View this message in context: http://r.789695.n4.nabble.com/Coverage-probability-for-a-Poisson-parameter-tp4702535p4702909.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Plotting dataset
Hi, Here is my implementation. Modify the data as follows so that it can be read with read.table and save as rHelp_20151206.txt under working directory. female male vowel language 391 339 i W.Apache 561 512 e W.Apache 826 670 a W.Apache 453 427 o W.Apache 358 291 i CA.English 454 406 e CA.English 991 706 a CA.English 561 439 o CA.English 398 324 u CA.English 334 307 i Ndumbea 444 361 e Ndumbea 796 678 a Ndumbea 542 474 o Ndumbea 333 311 u Ndumbea 343 293 i Sele 520 363 e Sele 989 809 a Sele 507 367 o Sele 357 300 u Sele Then read following the step below to get the plot: plot.case - read.table(rHelp_20150206.txt,header=TRUE) language.vowel - paste(language, vowel, sep=/) plot.case - data.frame(plot.case,language.vowel) ggplot(plot.case,aes(x=language.vowel,y=female)) + geom_bar(stat=identity,fill=lightblue,color=black) + coord_flip() -- View this message in context: http://r.789695.n4.nabble.com/Plotting-dataset-tp4702896p4702912.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Plotting dataset
Hi, Forgot to mention that ggplot2 needs to be installed: install.packages(ggplot2) Warning in install.packages : downloaded length 227 != reported length 227 trying URL 'http://cran.rstudio.com/bin/windows/contrib/3.1/ggplot2_1.0.0.zip' Content type 'application/zip' length 2675344 bytes (2.6 Mb) opened URL downloaded 2.6 Mb package ‘ggplot2’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\p068244\AppData\Local\Temp\Rtmp4WVORN\downloaded_packages library(ggplot2) -- View this message in context: http://r.789695.n4.nabble.com/Plotting-dataset-tp4702896p4702913.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Rotate array by 90°
Hi, Here is an implementation. rot90 function(a,times=1) { row - dim(a)[1] col - dim(a)[2] dep - dim(a)[3] if (times %% 2 == 1){t - row; row - col; col - t} tempA - array(NA, c(row,col,dep)) for (i in 1:dep) { temp - a[,,i] for (j in 1:times) { temp - cbind(sapply(1:row,function(x){rev(temp[x,])})) } tempA[,,i] - temp } return(tempA) } A , , 1 [,1] [,2] [,3] [1,] a b c [2,] d e f [3,] g h i , , 2 [,1] [,2] [,3] [1,] j k l [2,] m n o [3,] p q r rot90(A,3) , , 1 [,1] [,2] [,3] [1,] g d a [2,] h e b [3,] i f c , , 2 [,1] [,2] [,3] [1,] p m j [2,] q n k [3,] r o l -- View this message in context: http://r.789695.n4.nabble.com/Rotate-array-by-90-tp4702862p4702870.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Get 10%, 50% and 90% Quantile of data samples with probability
Hi, To complete the last part: dist - function(x) + { + return(c(rep(x[1],15),rep(x[2],15),rep(x[3],25),rep(x[4],20),rep(x[5],25))) + } x - dist(c(2,3,2,5.3,7.3)) x [1] 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 [29] 3.0 3.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 5.3 [57] 5.3 5.3 5.3 5.3 5.3 5.3 5.3 5.3 5.3 5.3 5.3 5.3 5.3 5.3 5.3 5.3 5.3 5.3 5.3 7.3 7.3 7.3 7.3 7.3 7.3 7.3 7.3 7.3 [85] 7.3 7.3 7.3 7.3 7.3 7.3 7.3 7.3 7.3 7.3 7.3 7.3 7.3 7.3 7.3 7.3 quantile(x, prob=c(0.1,0.5,0.9)) 10% 50% 90% 2.0 3.0 7.3 -- View this message in context: http://r.789695.n4.nabble.com/Get-10-50-and-90-Quantile-of-data-samples-with-probability-tp4702717p4702792.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] collapse a list of dataframes
Hi, The following worked. data.frame(rbind(as.matrix(data.frame(a=1:3,b=letters[1:3])),as.matrix(data.frame(x=1:5,b=LETTERS[1:5] a b 1 1 a 2 2 b 3 3 c 4 1 A 5 2 B 6 3 C 7 4 D 8 5 E -- View this message in context: http://r.789695.n4.nabble.com/collapse-a-list-of-dataframes-tp4702709p4702773.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Get 10%, 50% and 90% Quantile of data samples with probability
Hi, For you first task and use your first row of data as an example. Define a function named dist to take care of the probability for each group. dist - function(x) + { + return(c(rep(x[1],15),rep(x[2],15),rep(x[3],25),rep(x[4],20),rep(x[5],25))) + } plot(ecdf(dist(c(2,3,2,5.3,7.3 -- View this message in context: http://r.789695.n4.nabble.com/Get-10-50-and-90-Quantile-of-data-samples-with-probability-tp4702717p4702774.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] help plotting my data
Hi, I hope the following works for you. The plot is: Rplot.png http://r.789695.n4.nabble.com/file/n4702679/Rplot.png data - read.table(rHelp_20150202.txt,header=TRUE) order.data - order(data$counts,decreasing=TRUE) order.data [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 plot(data$counts ~ factor(data$role,levels=data$role[order.data])) -- View this message in context: http://r.789695.n4.nabble.com/help-plotting-my-data-tp4702583p4702679.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Dropping time series observations
Hi, I have a data frame named data and with the statement data[5:length(data[[1]]),] I can get row 5 to the end of the data. data role counts 1 Agent220 2 Theme169 3 Patient 67 4 Location 41 5 Destination 32 6 Recipient 31 7Result 29 8Instrument 27 9Source 25 10 Experiencer 22 11Topic 22 12 Stimulus 18 13Attribute 15 14 Beneficiary 12 15 Initial_Location 12 16 Co-Agent 11 17 Co-Patient 11 18 Co-Theme 9 19 Goal 9 20Asset 7 21Cause 6 22 Material 5 23Value 5 24 Product 4 25Predicate 3 26 Trajectory 3 27 Extent 2 28 Time 2 29Reflexive 1 data[5:length(data[[1]]),] role counts 5 Destination 32 6 Recipient 31 7Result 29 8Instrument 27 9Source 25 10 Experiencer 22 11Topic 22 12 Stimulus 18 13Attribute 15 14 Beneficiary 12 15 Initial_Location 12 16 Co-Agent 11 17 Co-Patient 11 18 Co-Theme 9 19 Goal 9 20Asset 7 21Cause 6 22 Material 5 23Value 5 24 Product 4 25Predicate 3 26 Trajectory 3 27 Extent 2 28 Time 2 29Reflexive 1 -- View this message in context: http://r.789695.n4.nabble.com/Dropping-time-series-observations-tp4702589p4702680.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Coverage probability for a Poisson parameter
Hi, Roughly reading the code, I find this statement phat - x / m is probably incorrect since this will give you the set of 100 observed x values /100. I redefine the function cover with three inputs: lambda for the parameter of the poisson distribution, sample.size and significance.level. The output is 1 or 0, depending on whether lambda is inside the confidence interval or not. With 5% level of significance I expect to get 95% of the time the parameter will be included in the confidence interval. The two runs below shows 96% and 94.8%, pretty close. cover - function(lambda, sample.size, significance.level) + { + x - rpois(sample.size,lambda) + estimate - mean(x) + lower - estimate - qnorm(1 - significance.level/2) * sqrt(estimate/sample.size) + upper - estimate + qnorm(1 - significance.level/2) * sqrt(estimate/sample.size) + if (lambda lower lambda upper){1}else{0} + } mean(sapply(1:100, function(x)cover(2.5,100,0.05))) [1] 0.96 mean(sapply(1:1000, function(x)cover(2.5,10,0.05))) [1] 0.948 -- View this message in context: http://r.789695.n4.nabble.com/Coverage-probability-for-a-Poisson-parameter-tp4702535p4702537.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] quetion about matrix compute
Hi, Here is my implementation. Hope this helps. b [1] 1 2 3 4 5 c [1] 1 2 1 3 5 4 sapply(b,function(x)ifelse(x==c,1,0)) [,1] [,2] [,3] [,4] [,5] [1,]10000 [2,]01000 [3,]10000 [4,]00100 [5,]00001 [6,]00010 -- View this message in context: http://r.789695.n4.nabble.com/quetion-about-matrix-compute-tp4702505p4702532.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Coverage probability for a Poisson parameter
Hi, My first question is what is your n when you say fixed n. I assume the lambda is the mean of the poisson distribution that you want to take sample from. Another question is about the sample size. It does not make too much sense to make a sample of size 1. Let's assume that you want to fix the sample size to be 100 and change lambda from 0.1 to 5 with an increment of 0.1. For each lambda, plan to run, say, 1000 times. Then the following will be my approach. Recall that the function cover returns 1 when lambda is in the confidence interval and 0 otherwise. resulting_matrix is created with size 50 x 2 with 0 populated. The matrix is to store lambda and the proportion of samples with lambda inside the confidence interval calculated from samples. With the resulting matrix, one can see that lambdas are in the first column with values of 0.1 to 5 with increment of 0.1. The corresponding proportions are in the second column. All of the proportions are from 0.917 to 0.969 as the last line shows. Hope this helps. cover - function(lambda, sample.size, significance.level) { + x - rpois(sample.size,lambda) + estimate - mean(x) + lower - estimate - qnorm(1 - significance.level/2) * sqrt(estimate/sample.size) + upper - estimate + qnorm(1 - significance.level/2) * sqrt(estimate/sample.size) + if (lambda lower lambda upper){1}else{0} + } resulting.matrix - matrix(0, nrow=50,ncol=2) for (i in 1:50) + { + resulting.matrix[i,1] - 0.1 * i + resulting.matrix[i,2] - mean(sapply(1:1000,function(x) cover(0.1*i,100,0.05))) + } resulting.matrix [,1] [,2] [1,] 0.1 0.917 [2,] 0.2 0.949 [3,] 0.3 0.928 [4,] 0.4 0.939 [5,] 0.5 0.943 [6,] 0.6 0.949 [7,] 0.7 0.942 [8,] 0.8 0.939 [9,] 0.9 0.945 [10,] 1.0 0.943 [11,] 1.1 0.962 [12,] 1.2 0.933 [13,] 1.3 0.947 [14,] 1.4 0.951 [15,] 1.5 0.946 [16,] 1.6 0.939 [17,] 1.7 0.946 [18,] 1.8 0.953 [19,] 1.9 0.964 [20,] 2.0 0.943 [21,] 2.1 0.937 [22,] 2.2 0.944 [23,] 2.3 0.945 [24,] 2.4 0.950 [25,] 2.5 0.954 [26,] 2.6 0.946 [27,] 2.7 0.945 [28,] 2.8 0.949 [29,] 2.9 0.956 [30,] 3.0 0.953 [31,] 3.1 0.941 [32,] 3.2 0.949 [33,] 3.3 0.943 [34,] 3.4 0.956 [35,] 3.5 0.950 [36,] 3.6 0.944 [37,] 3.7 0.952 [38,] 3.8 0.958 [39,] 3.9 0.938 [40,] 4.0 0.944 [41,] 4.1 0.950 [42,] 4.2 0.945 [43,] 4.3 0.948 [44,] 4.4 0.962 [45,] 4.5 0.969 [46,] 4.6 0.956 [47,] 4.7 0.950 [48,] 4.8 0.955 [49,] 4.9 0.946 [50,] 5.0 0.945 range(resulting.matrix[,2]) [1] 0.917 0.969 -- View this message in context: http://r.789695.n4.nabble.com/Coverage-probability-for-a-Poisson-parameter-tp4702535p4702551.html Sent from the R help mailing list archive at Nabble.com. __ 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 http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.