intakesKCal
of these four values.intakesKCal <- c( 2314, 2178, 1922, 2004 ) intakesKCal class( intakesKCal ) intakesKCal * 4.184
c()
to append the new intakes after values in intakesKCal
and store the result in allIntakesKCal
.length
. intakesKCal2 <- c( 2122, 2616, NA, 1771 ) allIntakesKCal <- c( intakesKCal, intakesKCal2 ) allIntakesKCal length( allIntakesKCal )
mean
intake for patients in vector intakesKCal
.mean
intake for patients in vector allIntakesKCal
.?mean
, in particular the na.rm
argument.na.rm=TRUE
to calculate the mean
of non-NA
elements of allIntakesKCal
.?sum
how to omit NA
elements in sum calculation.sum
of allIntakesKCal
intakes ignoring the NA
element.mean( intakesKCal ) mean( allIntakesKCal ) # since one element is missing, the mean is unknown # ?mean, adding argument na.rm=TRUE will omit NA elements mean( allIntakesKCal, na.rm = TRUE ) # ?sum also allows na.rm=TRUE argument to skip NA elements sum( allIntakesKCal, na.rm = TRUE )
is.na( allIntakesKCal )
.!
operator.sum
to calculate the number of missing and non-missing elements in allIntakesKCal
.allIntakesKCal[ !is.na( allIntakesKCal ) ]
.is.na( allIntakesKCal ) # TRUE marks positions with missing data !is.na( allIntakesKCal ) # TRUE marks positions with available data sum( is.na( allIntakesKCal ) ) # number of missing elements sum( !is.na( allIntakesKCal ) ) # number of non-missing elements allIntakesKCal[ !is.na( allIntakesKCal ) ] # keeps elements which are not NA sum( allIntakesKCal[ !is.na( allIntakesKCal ) ] ) # same as sum( allIntakesKCal, na.rm = TRUE )
v <- rnorm( 10 )
would sample 10 numbers from the normal distribution and store them as a vector in v
.v
.
Then repeat v <- rnorm( 10 )
and print v
again. Has v
changed?rnorm
and find how to generate random numbers with given mean
and standard deviation (sd
).v
simulate kcal intake by generating 15 random numbers with mean=2000
and sd=300
.v
and find by eye the smallest and the largest of these numbers.min
and max
on v
-- have you found the same numbers by eye?mean
, median
and the standard deviation (sd
) of v
.v <- rnorm( 10 ) # a vector of random numbers v v <- rnorm( 10 ) # another vector of random numbers v v <- rnorm( n = 15, mean = 2000, sd = 300 ) v min( v ) max( v ) mean( v ) # is it close to 2000? try several random v vectors and see the effect of growing n median( v ) sd( v ) # is it close to 300? try several random v vectors and see the effect of growing n
v
simulate kcal intake by generating 15 random numbers with mean=2000
and sd=300
.v < 2000
and understand the result.sum( v < 2000 )
?sum( !( v < 2000 ) )
? v <- rnorm( n = 15, mean = 2000, sd = 300 ) v v < 2000 # TRUE corresponds to elements of vector v SMALLER THAN 2000 v[ v < 2000 ] # selected elements of v smaller than 2000 sum( v < 2000 ) # number of elements in vector v smaller than 2000 sum( !( v < 2000 ) ) # number of elements in vector v GREATER OR EQUAL than 2000 sum( v >= 2000 ) # same as above
head
) or at the end (tail
) of a vector.v <- rnorm( 20 )
.v <- rnorm( 20 ) v head( v, 5 ) tail( v, 7 )
intakesKCal
of these eight values (in the given order).intakesKCal
.:
) to get the elements from the second to the fifth (inclusive).poses
with values 1, 3, 5, 7. Use it get the 1st, 3rd, 5th and 7th element of intakesKCal
.intakesKCal
typing numbers directly inside [...]
(without using an extra poses
variable).intakesKCal <- c( 2122, 2616, NA, 1771, 2314, 2178, 1922, 2004 ) intakesKCal intakesKCal[ 4 ] intakesKCal[ 2:5 ] poses <- c(1,3,5,7) intakesKCal[ poses ] intakesKCal[ c(1,3,5,7) ]
Help
pane) about seq
function.seq( 10, -5, 3 )
.seq( 10, -5, -3 ) seq( from = 10, to = -5, by = -3 )
Help
pane) about rep
function.rep( c( 0, 0, 1 ), 3 )
1380 2589 1586 2622 2849 2226
3. Type conversion to a character vector.
Sometimes it is necessary to convert a numerical vector to a character vector.
Understand what the function as.character
does for argument 1:5
.
1:5 as.character( 1:5 ) class( 1:5 ) class( as.character( 1:5 ) )
as.numeric
does for argument c( "1", "-1", "x" )
.NA
?as.numeric( c( "1", "-1", "x" ) )
ages <- c( Amy = 10, 'Dan' = 6, "Eve" = 11, "Eve 2" = 3, Grandma = NA )
.ages
and understand names( ages )
.Dan
. Try also for Eve 2
.ages <- c( Amy = 10, 'Dan' = 6, "Eve" = 11, "Eve 2" = 3, Grandma = NA ) ages names( ages ) ages[ 'Dan' ] ages[ 'Eve 2' ] # Another way to create a vector with named elements ages2 <- c( 10, 6, 11, 3, NA ) names( ages2 ) <- c( "Amy", "Dan", "Eve", "Eve 2", "Grandma" ) ages2
test.txt
) and store it in the variable fileName
.v
with several text elements.writeLines
and try writeLines(v)
to see in the console what will be written to a file.con = fileName
and write to the file.readLines( con = fileName )
to read the file and put it back to variable w
.identical( v, w )
. fileName <- "test.txt" v <- c( "First line", "Second", "Third", "4th", "5th", "6th" ) v writeLines( v ) # writes to the console writeLines( v, con = fileName ) # writes to a file w <- readLines( con = fileName ) identical( v, w ) # checks whether v and w are exactly equal unlink( fileName ) # removes the file
v
to be a vector of some numbers.as.character
to make writeLines
work (do not change v
).identical( v, w )
fails? Check class(v)
and class(w)
.w
would be needed to make identical
work?fileName <- "test.txt" v <- sample( 1:100, 10 ) v writeLines( as.character( v ) ) # conversion to character needed writeLines( as.character( v ), con = fileName ) w <- readLines( con = fileName ) identical( v, w ) # numbers are not the same as their text representation w <- as.numeric( w ) identical( v, w ) # still not identical; class(v) is different than class(w) w <- as.integer( w ) identical( v, w ) # now identical unlink( fileName ) # removes the file
(ADV) Merging data from corresponding vectors.
Let's assume that we have data on incomes and spendings of several persons.
The data are provided in three vectors: nms
, incomes
and spendings
(as shown below).
One person is described by corresponding elements of the three vectors.
Find a way to calculate:
balances
: (income minus spending) for each person;balances
in descending order and print the names of persons corresponding to this order.Hints: which.max
, names
, sort
, decreasing
.
nms <- c( "Amy", "Bob", "Carl", "Dany", "Ela", "Fred" ) incomes <- c( 1380, 2589, 1586, 2622, 2849, 2226 ) spendings <- c( 1198, 2111, 1224, 780, 3266, 2200 )
balance <- incomes - spendings balance max(balance) which.max(balance) nms[which.max(balance)] names(balance) <- nms balance sort(balance) sort(balance, decreasing = TRUE) names(sort(balance, decreasing = TRUE))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.