[R] browser() question
All, How come i=1 in the first case, but i=2 in the second case. The second case seems to work, but the first case does not. David. > findruns <- function(x,k) { + n <- length(x) + runs <- NULL + for (i in 1:(n-k+1)) { + if (all(x[i:(i+k-1)]==1)) runs <- c(runs,i) + browser(i>1) + } + return(runs) + } > x=c(1,1,0,0,1,1,1) > findruns(x,2) Called from: findruns(x, 2) Browse[1]> i [1] 1 Browse[1]> Q > findruns <- function(x,k) { + n <- length(x) + runs <- NULL + for (i in 1:(n-k+1)) { + if (all(x[i:(i+k-1)]==1)) runs <- c(runs,i) + if (i>1) browser() + } + return(runs) + } > findruns(x,2) Called from: findruns(x, 2) Browse[1]> i [1] 2 Browse[1]> -- View this message in context: http://r.789695.n4.nabble.com/browser-question-tp4636104.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Adjusting format of boxplot
Looks like data was not attached. Here is is. longgroup 40 1 37 1 44 1 47 1 47 1 47 1 68 1 47 1 54 1 61 1 71 1 75 1 89 1 58 1 59 1 62 1 79 1 96 1 58 1 62 1 70 1 72 1 74 1 96 1 75 1 46 2 42 2 65 2 46 2 58 2 42 2 48 2 58 2 50 2 80 2 63 2 65 2 70 2 70 2 72 2 97 2 46 2 56 2 70 2 70 2 72 2 76 2 90 2 76 2 92 2 21 3 40 3 44 3 54 3 36 3 40 3 56 3 60 3 48 3 53 3 60 3 60 3 65 3 68 3 60 3 81 3 81 3 48 3 48 3 56 3 68 3 75 3 81 3 48 3 68 3 35 4 37 4 49 4 46 4 63 4 39 4 46 4 56 4 63 4 65 4 56 4 65 4 70 4 63 4 65 4 70 4 77 4 81 4 86 4 70 4 70 4 77 4 77 4 81 4 77 4 16 5 19 5 19 5 32 5 33 5 33 5 30 5 42 5 42 5 33 5 26 5 30 5 40 5 54 5 34 5 34 5 47 5 47 5 42 5 47 5 54 5 54 5 56 5 60 5 44 5 -- View this message in context: http://r.789695.n4.nabble.com/Adjusting-format-of-boxplot-tp4636373p4636379.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Adjusting format of boxplot
Added your code: flies <- read.table("example12_1.dat",header=TRUE,sep="\t") flies$group <- factor(flies$group,5:1) levels(flies$group) <- paste0("Group ",5:1) boxplot(long ~ group, data = flies, pars = list(las=1, ylim=c(10,110), xaxt="n", bty="n"), horizontal = TRUE, col = "red") axis(1,at=seq(10,110,20)) Almost worked perfectly, except the frame around the plot remains, which is strange as you have bty="n". http://r.789695.n4.nabble.com/file/n4636381/Rplot11.png David -- View this message in context: http://r.789695.n4.nabble.com/Adjusting-format-of-boxplot-tp4636373p4636381.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Side by side strip charts
Hi, I'm looking for some ideas on how to reproduce the attached image in R. There are three samples, each of size n = 10. The first is drawn from a normal distribution with mean 60 and standard deviation 3. The second is drawn from a normal distribution with mean 65 and standard deviation 3. The third is drawn from a normal distribution with mean 70 and standard deviation 3. http://r.789695.n4.nabble.com/file/n4636399/IMG_1306.jpg Any ideas? Thanks. David. -- View this message in context: http://r.789695.n4.nabble.com/Side-by-side-strip-charts-tp4636399.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Side by side strip charts
OK, got this far: x1 <- round(rnorm(10,60,3)) x2 <- round(rnorm(10,65,3)) x3 <- round(rnorm(10,70,3)) stripchart(list(sample1=x1,sample2=x2,sample3=x3), method="stack", pch=4, offset=1/2, col="blue", lwd=2, las=1) Any ideas on how to get an axes drawn under each one as in the image? Thanks. David Arnold College of the Redwoods http://msemac.redwoods.edu/~darnold/index.php -- View this message in context: http://r.789695.n4.nabble.com/Side-by-side-strip-charts-tp4636399p4636464.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Side by side strip charts
Very nice suggestion. I am getting some very kind help here. x1 <- round(rnorm(10,60,3)) x2 <- round(rnorm(10,65,3)) x3 <- round(rnorm(10,70,3)) stripchart(list(sample1=x1,sample2=x2,sample3=x3), method="stack", pch=4, offset=1/2, col="blue", lwd=2, las=1, xlim=c(50,80)) axis(1,pos=2.9,labels=FALSE) axis(1,pos=1.9,labels=FALSE) http://r.789695.n4.nabble.com/file/n4636502/Rplot.png Thanks. David Arnold College of the Redwoods -- View this message in context: http://r.789695.n4.nabble.com/Side-by-side-strip-charts-tp4636399p4636502.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Arrange two columns into a five variable dataframe
Hi, I hope that folks can give me some simple approaches to taking the data set below, which is accumulated in two columns called "long" and "group", then arrange the data is the "long" column into a data frame containing five variables: "Group 1", "Group 2", "Group 3", "Group 4", and "Group 5". I am hoping for a few different techniques which I can pass on to my students. Thanks David Arnold College of the Redwoods > dput(flies) structure(list(long = c(40L, 37L, 44L, 47L, 47L, 47L, 68L, 47L, 54L, 61L, 71L, 75L, 89L, 58L, 59L, 62L, 79L, 96L, 58L, 62L, 70L, 72L, 74L, 96L, 75L, 46L, 42L, 65L, 46L, 58L, 42L, 48L, 58L, 50L, 80L, 63L, 65L, 70L, 70L, 72L, 97L, 46L, 56L, 70L, 70L, 72L, 76L, 90L, 76L, 92L, 21L, 40L, 44L, 54L, 36L, 40L, 56L, 60L, 48L, 53L, 60L, 60L, 65L, 68L, 60L, 81L, 81L, 48L, 48L, 56L, 68L, 75L, 81L, 48L, 68L, 35L, 37L, 49L, 46L, 63L, 39L, 46L, 56L, 63L, 65L, 56L, 65L, 70L, 63L, 65L, 70L, 77L, 81L, 86L, 70L, 70L, 77L, 77L, 81L, 77L, 16L, 19L, 19L, 32L, 33L, 33L, 30L, 42L, 42L, 33L, 26L, 30L, 40L, 54L, 34L, 34L, 47L, 47L, 42L, 47L, 54L, 54L, 56L, 60L, 44L ), group = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Group 5", "Group 4", "Group 3", "Group 2", "Group 1"), class = "factor")), .Names = c("long", "group"), row.names = c(NA, -125L), class = "data.frame") -- View this message in context: http://r.789695.n4.nabble.com/Arrange-two-columns-into-a-five-variable-dataframe-tp4636503.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Regression Identity
Hi, I see a lot of folks verify the regression identity SST = SSE + SSR numerically, but I cannot seem to find a proof. I wonder if any folks on this list could guide me to a mathematical proof of this fact. Thanks. David. -- View this message in context: http://r.789695.n4.nabble.com/Regression-Identity-tp4636829.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Entering Data Files
Hi, Entering data from a given file is the hardest part of learning R for me. For example, I have this datafile from Moore's text: LiveAge Count Parents Age19 324 Another Age19 37 OwnPlaceAge19 116 Group Age19 58 Other Age19 5 Parents Age20 378 Another Age20 47 OwnPlaceAge20 279 Group Age20 60 Other Age20 2 Parents Age21 337 Another Age21 40 OwnPlaceAge21 372 Group Age21 49 Other Age21 3 Parents Age22 318 Another Age22 38 OwnPlaceAge22 487 Group Age22 25 Other Age22 9 I can manually enter the numbers and create my table and barplot. x <- matrix(c(324, 378, 337, 318, 37, 47, 40, 38, 116, 279, 372, 487, 58, 60, 49, 25, 5, 2, 3, 9), c(5,4),byrow=T) rownames(x)=c("Parents","Another","OwnPlace","Group","Other") colnames(x)=c("Age19","Age20","Age21","Age22") x <- as.table(x) barplot(x,beside=T) But it would be nice to be able to read the data from the file, arrange it using R, then plot, if that is possible. Any suggestions? Thanks David -- View this message in context: http://r.789695.n4.nabble.com/Entering-Data-Files-tp4636943.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Last answer
Hi, In Matlab, I can access the last computation as follows: >> 2+3 ans = 5 >> ans ans = 5 Anything similar in R? David -- View this message in context: http://r.789695.n4.nabble.com/Last-answer-tp4637151.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Last answer
All, Thanks. That works. D. -- View this message in context: http://r.789695.n4.nabble.com/Last-answer-tp4637151p4637157.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Library control
All, 1. Is there a command that tells you what libraries are loaded? 2. How do you remove a library that has been loaded? 3. If you have loaded a library that masks previously loaded objects, how can you tell which object is being run? In Matlab, I would run "which f" to determine the location of the file f that will be run. Is there a similar procedure in R? David. -- View this message in context: http://r.789695.n4.nabble.com/Library-control-tp4637327.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] rhsape2 bug?
All, I believe I am running the latest version of rshape2 (1.2.1). But this code: library(reshape2) tmp <- melt(smiths, id.vars=1:2, measure.vars=c("age","weight","height"), variable.name="myvars", value.name="myvals" ) names(tmp) Produces this output: > names(tmp) [1] "subject" "time" "variable" "value" So you can see that these lines of code don't work: variable.name="myvars", value.name="myvals" Is this a bug? David -- View this message in context: http://r.789695.n4.nabble.com/rhsape2-bug-tp4637331.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] rhsape2 bug?
Ista, You are correct. > search() [1] ".GlobalEnv""package:reshape" "package:plyr" [4] "package:reshape2" "tools:rstudio" "package:stats" [7] "package:graphics" "package:grDevices" "package:utils" [10] "package:datasets" "package:methods" "Autoloads" [13] "package:base" Thanks. D. -- View this message in context: http://r.789695.n4.nabble.com/rhsape2-bug-tp4637331p4637337.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Printing a summary
All, Is this typical of how people will print a summary of results? CoinTosses <- function(n) { x <- sample(c(0,1), n, replace=TRUE) y <- x y[y==0] <- "T" y[y==1] <- "H" numHeads <- sum(x) numTails <- n-sum(x) p <- numHeads/n cat(cat(y,sep=""),"\n") cat("Number of heads: ", numHeads, "\n") cat("Number of tails: ", numTails, "\n") cat("The proportion of heads is: ", p, "\n") } CoinTosses(40) Or is another technique preferred? Thanks. David -- View this message in context: http://r.789695.n4.nabble.com/Printing-a-summary-tp4639104.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Printing contents of a variable
All, Can someone explain why this does not print the contents of x when I source this file? CoinTosses <- function(n,print=TRUE) { x <- sample(c(0,1), n, replace=TRUE) y <- x y[y==0] <- "T" y[y==1] <- "H" p <- sum(x)/n p } x <- CoinTosses(40) x On the other hand, if I source this file: CoinTosses <- function(n,print=TRUE) { x <- sample(c(0,1), n, replace=TRUE) y <- x y[y==0] <- "T" y[y==1] <- "H" p <- sum(x)/n p } Then run at the command line: x <- CoinTosses(40) x The result is: > x <- CoinTosses(40) > x [1] 0.475 Not sure why the former does not display the contents of x. Thanks. D. -- View this message in context: http://r.789695.n4.nabble.com/Printing-contents-of-a-variable-tp4639106.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Head or Tails game
Hi, Reading about a "Heads and Tails" game in http://www.dartmouth.edu/~chance/teaching_aids/books_articles/probability_book/amsbook.mac.pdf Introduction to Probability (Example 1.4, pp. 5-8). You toss a coin 40 times. If heads, Peter wins $1, tails, he loses $1. I think I can do that ok with: winnings <- sum(sample(c(-1,1), 40, replace=TRUE)) But I have to do it 10,000 times and I have to record and collect the winnings. In other languages, I would probably use a for loop and "push" each winnings into some sort of collective array or vector. However, for loops seem to be discouraged in R and it seems the same can be said for "pushing" a calculation onto a vector. So, can someone give me some guidance on how to collect 10,000 winnings? The second part of the game asks us to keep track of how often Peter is in the lead during each game. Obviously, he is in the lead at any moment his cumulative winnings are positive. But the game requires that we also do something at the moment the cumulative winnings are zero. (1) if the previous cumulative sum was nonnegative, then the zero counts a "staying in the lead." So, for example, during a single game, Peter might be in the lead for say 34 out of the 40 tosses. I must record the 34 and perform the game 9,999 more times, each time recording the number of times that Peter is in the lead. So again, any thoughts on how to do this without for loops and "pushing?" Thanks for the help. Great list. David Arnold College of the Redwoods Eureka, CA http://msemac.redwoods.edu/~darnold/index.php -- View this message in context: http://r.789695.n4.nabble.com/Head-or-Tails-game-tp4639142.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Head or Tails game
David, set.seed(123) # always good to make reproducible winnings <- sum(sample(c(-1,1), 1, replace=TRUE)) Unfortunately, that's not the game. The game requires 40 flips of a coin. Then you have to play the game 10,000 times. D. -- View this message in context: http://r.789695.n4.nabble.com/Head-or-Tails-game-tp4639142p4639145.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Head or Tails game
Wow! Some great responses! I am getting some great responses. I've only read David, Michael, and Dennis thus far, leading me to develop this result before reading further. lead <- function(x) { n <- length(x) count <- 0 if (x[1] >= 0) count <- count + 1 for (i in 2:n) { if (x[i] > 0 || (x[i] == 0 && x[i-1] >= 0 )) { count <- count + 1 } } count } games <- replicate(1,sample(c(-1,1),40,replace=TRUE)) games_sum <- apply(games,2,sum) plot(table(games_sum)) games_lead <- apply(games,2,cumsum) games_lead <- apply(games_lead,2,lead) plot(table(games_lead)) Now I am going to read Arun, William, and Jeff's responses and see what other ideas are being proposed. Thanks everyone. D. -- View this message in context: http://r.789695.n4.nabble.com/Head-or-Tails-game-tp4639142p4639150.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Reading one column .csv file
My friend sent an Excel file: http://msemac.redwoods.edu/~darnold/temp/cyu01_iqscores.xls http://msemac.redwoods.edu/~darnold/temp/cyu01_iqscores.xls I opened it in Excel, saved is as cyu01_iqscores.csv, then imported it into R with: iqscores=read.csv('cyu01_iqscores.csv',header=TRUE) The result was: > head(iqscores) IQ.Scores X 1 145 NA 2 101 NA 3 123 NA 4 106 NA 5 117 NA 6 102 NA Now, I know I can cure this with: iqscores=iqscores[,1] But I am wondering about this weird behavior. Suggestions? David -- View this message in context: http://r.789695.n4.nabble.com/Reading-one-column-csv-file-tp4640396.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Reading one column .csv file
Peter, Interesting. Never heard of copying from the clipboard. I am also on a MacBook Pro, but I cannot get it to work. 1. I selected the column of data (including the header) in Excel. 2. Selected Edit->Copy. 3. In R, tried: > a <- read.delim("clipboard") Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : clipboard cannot be opened or contains no text 4. I checked the clipboard. The data is there. What am I missing? D. -- View this message in context: http://r.789695.n4.nabble.com/Reading-one-column-csv-file-tp4640396p4640429.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Reading one column .csv file
Tried that already. My clipboard: x 2 3 4 5 My attempt: > a <- read.delim(pipe("pbpaste")) Warning message: In read.table(file = file, header = header, sep = sep, quote = quote, : incomplete final line found by readTableHeader on 'pbpaste' And again: > a <- read.delim(pipe("pbpaste"),header=TRUE) Warning message: In read.table(file = file, header = header, sep = sep, quote = quote, : incomplete final line found by readTableHeader on 'pbpaste' Suggestions? David -- View this message in context: http://r.789695.n4.nabble.com/Reading-one-column-csv-file-tp4640396p4640456.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Reading one column .csv file
Worked! Thanks. David. -- View this message in context: http://r.789695.n4.nabble.com/Reading-one-column-csv-file-tp4640396p4640462.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Beside Barplot
All, Is there a way to get a barplot with beside for the following without reshaping mydata? opinion <- c("Almost no chance", "Probably Not","50-50 Chance","A good chance","Almost certain") female <- c(96,426,696,663,486) male <- c(98,286,720,758,597) mydata<-data.frame(opinion,female,male) Thanks David -- View this message in context: http://r.789695.n4.nabble.com/Beside-Barplot-tp4640807.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Turning of source echo
HI, I have this code: BAGA <- c(10,20,20,30,30,30,30,40, 40,40,40,50,50,50,50,50, 60,60,60,60,60,70,70,70, 70,80,80,80,90,90,100) BAGB <- c(10,10,10,10,10,20,20,20, 20,30,30,30,40,40,50,60, 70,70,80,80,80,90,90,90, 90,100,100,100,100,100) A <- sample(c(1,2),1) if (A==1) { B <- BAGA[sample(length(BAGA),1)] } else { B <- BAGB[sample(length(BAGB),1)] } cat("X = ", B) In R-studio, when I source the code with Shift-Command-S on the mac, I get this response in the console window: > source('~/Documents/classes/trunk/math15/resources/TIFiles/todd.R') X = 90 How can I turn of the source echo to get just: X = 90 Thanks. D. -- View this message in context: http://r.789695.n4.nabble.com/Turning-of-source-echo-tp4641278.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Turning of source echo
Noia, Nope. Although sep is helpful for my output, I still get that source echo that I want to suppress. Here is the code with Noia's suggestion. BAGA <- c(10,20,20,30,30,30,30,40, 40,40,40,50,50,50,50,50, 60,60,60,60,60,70,70,70, 70,80,80,80,90,90,100) BAGB <- c(10,10,10,10,10,20,20,20, 20,30,30,30,40,40,50,60, 70,70,80,80,80,90,90,90, 90,100,100,100,100,100) A <- sample(c(1,2),1) if (A==1) { B <- BAGA[sample(length(BAGA),1)] } else { B <- BAGB[sample(length(BAGB),1)] } cat("X = ", B, sep="") And the output: > source('~/Documents/classes/trunk/math15/resources/TIFiles/todd.R') X = 60 I want to suppress that source echo. D. I want this (source('~/ac -- View this message in context: http://r.789695.n4.nabble.com/Turning-of-source-echo-tp4641278p4641305.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Turning of source echo
Berend, Yes, I did ask this question on RStudio help page. Josh suggested that I try to find some package that pops up a window with the results I want. Anyone know how to do that? Is there a package or a tutorial that will instruct me as to how to do that? Here is my code: BAGA <- c(10,20,20,30,30,30,30,40, 40,40,40,50,50,50,50,50, 60,60,60,60,60,70,70,70, 70,80,80,80,90,90,100) BAGB <- c(10,10,10,10,10,20,20,20, 20,30,30,30,40,40,50,60, 70,70,80,80,80,90,90,90, 90,100,100,100,100,100) A <- sample(c(1,2),1) if (A==1) { B <- BAGA[sample(length(BAGA),1)] } else { B <- BAGB[sample(length(BAGB),1)] } cat("X = ", B, ".\n",sep="") if (A==1) cat("The voucher is from Bag A.\n") if (A==2) cat("The voucher is from Bag B.\n") What I'd like to do is find a way to repeat this code N number of times, with the student hitting the enter key or the space bar to get the next repetition. Each time, I want a result similar to the following to come up, but I don't want that source message to come up. > source('~/.active-rstudio-document') X = 30. The voucher is from Bag A. Each time Anyone have any suggestions? Thanks. David. -- View this message in context: http://r.789695.n4.nabble.com/Turning-of-source-echo-tp4641278p4641317.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Two selections from Bag A
All, I am looking at an example in Aliaga's Interactive Statistics. Bag A has the following vouchers. BagA <- c(-1000,10,10,10,10,10,10, 10,20,20,20,20,20,20,30, 30,40,40,50,60) Bag B has the following vouchers. BagB <- c(10,20,30,30,40,40,50,50, 50,50,50,50,60,60,60,60, 60,60,60,1000) Two values are selected (from BagA or BagB) without replacement. In Table 1.1 on page 54 of the third edition, she lists all "Possible two values selected" in columns one and two, the "Average of the two selected values" in column three and "BAG A Numbers of way of selecting the two values" in column four, and "BAG B Number of ways of selecting the two values" in column five. Here are the first few rows: -1000 -1000 -1000 0 0 -1000 10 -495 7 0 -1000 20 -490 6 0 -100030 -485 2 0 -100040 -480 2 0 -100050 -475 1 0 -100060 -470 1 0 -1000 1000 0 0 0 10 10 10 21 0 1020 1542 1 ... She then condenses the data in Table 1.2 on page 55, the first column holding "Average of the two selected values', the second column holding "BAG A Number of ways of selecting the two values," and third column holding "BAG B Number of ways of selecting the two values." Here are a few sample rows: -1000 0 0 -495 7 0 -490 6 0 Can anyone help show me an efficient way of creating these two tables? Thanks. David. -- View this message in context: http://r.789695.n4.nabble.com/Two-selections-from-Bag-A-tp4641327.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Two selections from Bag A
Here are the two tables from Aligaga. The first is table 1.1 and the second is table 1.2. http://r.789695.n4.nabble.com/file/n4641344/table1_1.jpg http://r.789695.n4.nabble.com/file/n4641344/table1_2.jpg David Arnold College of the Redwoods -- View this message in context: http://r.789695.n4.nabble.com/Two-selections-from-Bag-A-tp4641327p4641344.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Aligning barplot
Hmmm This worked this time. And I didn't use the transform t. BagA <- c(-1000,10,10,10,10,10,10, 10,20,20,20,20,20,20,30, 30,40,40,50,60) BagB <- c(10,20,30,30,40,40,50,50, 50,50,50,50,60,60,60,60, 60,60,60,1000) tblA <- table(BagA) tblA <- c(tblA,0) names(tblA) <- c(names(table(BagA)),"1000") tblB <- table(BagB) tblB <- c(0,tblB) names(tblB) <- c("-1000",names(table(BagB))) layout(c(2,1)) barplot(tblB, main="Bag B") barplot(tblA, main="Bag A") D. David Winsemius wrote > > > darnold wrote >> >> All, Consider: >> >> BagA <- c(-1000,10,10,10,10,10,10, >> 10,20,20,20,20,20,20,30, >> 30,40,40,50,60) >> >> BagB <- c(10,20,30,30,40,40,50,50, >> 50,50,50,50,60,60,60,60, >> 60,60,60,1000) >> >> layout(c(2,1)) >> >> barplot(table(BagB)) >> barplot(table(BagA)) >> >> At this point, I'd like to arrange the plots so that the 10-bars are >> aligned, the 20-bars are aligned, etc. So, I started thinking, how do I >> add an entry to a table? I tried: >> >> tmp <- table(BagA) >> tmp[8] <- 0 >> names(tmp)[8] <- "1000" >> >> barplot(tmp) >> >> But I got this error: >> >> > > Try using t(tmp) when plotting. When you started mucking with that > table-object, it got converted (effectively) to a one row matrix (even > though it retained it "table"-class attribute) and you need to make it a > one column matrix to get the desired results with barplot(). > >> tmpB <- table(BagB) >> tmpB <- c(0, tmpB) >> names(tmpB) <- c(-1000, names(table(BagB)) ) > >> barplot( t(tmpB) ) >> barplot( t(tmp) ) > > > -- > David (W.) > -- View this message in context: http://r.789695.n4.nabble.com/Aligning-barplot-tp4641356p4641359.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Aligning barplot
I was able to do this, but kinda painful: BagA <- c(-1000,10,10,10,10,10,10, 10,20,20,20,20,20,20,30, 30,40,40,50,60) BagB <- c(10,20,30,30,40,40,50,50, 50,50,50,50,60,60,60,60, 60,60,60,1000) tblA <- table(BagA) tblAnames <- names(tblA) tblA <- as.vector(tblA) tblA[8] <- 0 names(tblA) <- c(tblAnames,"1000") tblB <- table(BagB) tblBnames <- names(tblB) tblB <- as.vector(tblB) tblB <- c(0,tblB) names(tblB) <- c("-1000",tblBnames) layout(c(2,1)) barplot(tblB) barplot(tblA) Any better ideas? darnold wrote > > All, Consider: > > BagA <- c(-1000,10,10,10,10,10,10, > 10,20,20,20,20,20,20,30, > 30,40,40,50,60) > > > BagB <- c(10,20,30,30,40,40,50,50, > 50,50,50,50,60,60,60,60, > 60,60,60,1000) > > layout(c(2,1)) > > barplot(table(BagB)) > barplot(table(BagA)) > > At this point, I'd like to arrange the plots so that the 10-bars are > aligned, the 20-bars are aligned, etc. So, I started thinking, how do I > add an entry to a table? I tried: > > tmp <- table(BagA) > tmp[8] <- 0 > names(tmp)[8] <- "1000" > > barplot(tmp) > > But I got this error: > >> source('~/.active-rstudio-document') > Error in barplot.default(tmp) : 'height' must be a vector or a matrix > > Makes me think I should not be telling my students to do: > > barplot(table(BagA)) > > As it might give future troubles. > > 1. What am I missing? > > 2. Can anyone propose an alternate strategy for aligning my tables? > > Thanks > > David Arnold > College of the Redwoods > -- View this message in context: http://r.789695.n4.nabble.com/Aligning-barplot-tp4641356p4641357.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Aligning barplot
All, Consider: BagA <- c(-1000,10,10,10,10,10,10, 10,20,20,20,20,20,20,30, 30,40,40,50,60) BagB <- c(10,20,30,30,40,40,50,50, 50,50,50,50,60,60,60,60, 60,60,60,1000) layout(c(2,1)) barplot(table(BagB)) barplot(table(BagA)) At this point, I'd like to arrange the plots so that the 10-bars are aligned, the 20-bars are aligned, etc. So, I started thinking, how do I add an entry to a table? I tried: tmp <- table(BagA) tmp[8] <- 0 names(tmp)[8] <- "1000" barplot(tmp) But I got this error: > source('~/.active-rstudio-document') Error in barplot.default(tmp) : 'height' must be a vector or a matrix Makes me think I should not be telling my students to do: barplot(table(BagA)) As it might give future troubles. 1. What am I missing? 2. Can anyone propose an alternate strategy for aligning by tables? Thanks David Arnold College of the Redwoods -- View this message in context: http://r.789695.n4.nabble.com/Aligning-barplot-tp4641356.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.