You are here

Group Exercise: R Syntax

Each group will write R code to compute the average of a series of numbers, assign that result to a variable, and then transform that variable with an arithmetic operation (addition, subtraction, multiplication, division). Test your code in R and when it works, copy and paste the code into a comment so we can discuss what you've done.

A. 2, 5, 5, 4, 9, 6, 10, 2, 0, 2, 19, 16, 9
B. 30, 22, 22, 21, 18, 17, 17, 16, 14, 14, 14, 11
C. 0, 2, 3, 4, 6, 7, 8, 9, 9, 11, 12, 14, 14, 15
D. 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23
E. 0.5, 0.5, 0.75, 1, 1.25, 1.25, 1.5, 1.75, 2, 2.25, 2.25, 2.5, 2.75, 3

Comments

> x<-c(2,5,5,4,9,6,10,2,0,2,19,16,9)
> mean(x)
[1] 6.846154
> x+12
[1] 14 17 17 16 21 18 22 14 12 14 31 28 21

> y<-c(1,3,5,7,9,11,13,15,17,19,21,23)
> mean(y)
[1] 12
> x<-mean(y)
> x*4
[1] 48

> x<-c(0, 2, 3, 4, 6, 7, 8, 9, 9, 11, 12, 14, 14, 15)
> mean(x)
[1] 8.142857
> y<- mean(x)
> y+9
[1] 17.14286

> x<-c(2,5,5,4,9,6,10,2,0,2,19,16,9)
> mean(x)
[1] 6.846154
> y<-mean(x)
> y-2
[1] 4.846154
> y+4
[1] 10.84615
> y-2+4
[1] 8.846154
> y-8/2
[1] 2.846154
> y+8/2+7
[1] 17.84615
> y+14/6
[1] 9.179487
> help
function (topic, package = NULL, lib.loc = NULL, verbose = getOption("verbose"),
try.all.packages = getOption("help.try.all.packages"), help_type = getOption("help_type"))
{
types <- c("text", "html", "pdf")
help_type <- if (!length(help_type))
"text"
else match.arg(tolower(help_type), types)
if (!missing(package))
if (is.name(y <- substitute(package)))
package <- as.character(y)
if (missing(topic)) {
if (!is.null(package)) {
if (interactive() && help_type == "html") {
port <- tools::startDynamicHelp(NA)
if (port <= 0L)
return(library(help = package, lib.loc = lib.loc,
character.only = TRUE))
browser <- if (.Platform$GUI == "AQUA") {
get("aqua.browser", envir = as.environment("tools:RGUI"))
}
else getOption("browser")
browseURL(paste0("http://127.0.0.1:", port, "/library/",
package, "/html/00Index.html"), browser)
return(invisible())
}
else return(library(help = package, lib.loc = lib.loc,
character.only = TRUE))
}
if (!is.null(lib.loc))
return(library(lib.loc = lib.loc))
topic <- "help"
package <- "utils"
lib.loc <- .Library
}
ischar <- tryCatch(is.character(topic) && length(topic) ==
1L, error = identity)
if (inherits(ischar, "error"))
ischar <- FALSE
if (!ischar) {
reserved <- c("TRUE", "FALSE", "NULL", "Inf", "NaN",
"NA", "NA_integer_", "NA_real_", "NA_complex_", "NA_character_")
stopic <- deparse(substitute(topic))
if (!is.name(substitute(topic)) && !stopic %in% reserved)
stop("'topic' should be a name, length-one character vector or reserved word")
topic <- stopic
}
paths <- index.search(topic, find.package(if (is.null(package))
loadedNamespaces()
else package, lib.loc, verbose = verbose))
try.all.packages <- !length(paths) && is.logical(try.all.packages) &&
!is.na(try.all.packages) && try.all.packages && is.null(package) &&
is.null(lib.loc)
if (try.all.packages) {
for (lib in .libPaths()) {
packages <- .packages(TRUE, lib)
packages <- packages[is.na(match(packages, .packages()))]
paths <- c(paths, index.search(topic, file.path(lib,
packages)))
}
paths <- paths[nzchar(paths)]
}
structure(unique(paths), call = match.call(), topic = topic,
tried_all_packages = try.all.packages, type = help_type,
class = "help_files_with_topic")
}

> help("table")
> x2<-c(0,2,3,4,6,7,8,9,9,11,12,14,14,15\)
Error: unexpected input in "x2<-c(0,2,3,4,6,7,8,9,9,11,12,14,14,15\"
> x2<-c(0,2,3,4,6,7,8,9,9,11,12,14,14,15)
> mean(x2)
[1] 8.142857
> y<-mean(x2)
> y-4X2
Error: unexpected symbol in "y-4X2"
> y-4*2
[1] 0.1428571

> x<-c(2,5,5,4,9,6,10,2,0,2,19,16,9)
> mean(x)
[1] 6.846154
> y<-mean(x)
> y-2
[1] 4.846154