R语言练习(1A)

本文介绍了如何在R语言中创建和处理变量,如数值型和字符串,使用is.numeric和round函数,以及创建向量、矩阵、数据框和列表。此外,还涉及了如何加载和管理R包,如安装palmerpenguins包并查看其帮助文件。
摘要由CSDN通过智能技术生成
1 Exercises

1.1

Create a numeric variable a using the R code below:

a <- 9

Run a to verify that your code has worked properly.

1.2

To test whether a is indeed a numeric variable, we can use the function is.numeric. Run the R code below, and interpret the output.

is.numeric(a)

1.3

Next, create a second object in R called b, and store the value 8 in this object.

1.4

Using *, multiply a by b in R, and assign the square root of the result to a third object called c.
Run c and is.numeric(c) to verify that your code has worked properly.

1.5

Suppose that we would like to round the value stored in c to 3 decimal places. Rather than doing this by hand, we can use the built-in R function round.
Throughout this subject, we will use many different R functions. Each will work differently, so it is always a good idea to check how to use a function before applying it to your data. The function is.numeric was fairly straightforward to understand (outputting true or false), but the function round is a little more complicated, as it requires multiple inputs aka arguments.
To bring up some helpful information on a function, you can type ? or help() and then the name of the function, and then run that line of code - try running the R code below for the round function:

?round
help(round)

1.6

Following what you have learned from the round help file in 1.5, use the round function to round the value stored in c to 3 decimal places.
What is your final answer for c?

1.7

Use the function seq to create a vector called fives of values from 0 to 40, which increase in increments of 5 (i.e. 0, 5, 10, 15, …).
After creating your vector, run fives to verify that it looks how you expect it to look. If something seems wrong with your result and you are not sure how to fix it, check with your demonstrator.

1.8

Next, create a vector containing the names of the following countries: Australia, Singapore, China, New Zealand, Japan and India. Name this vector countries.

1.9

Now create a vector called pop.millions containing the values 25, 5, 1433, 4, 126 and 1366, in that order.

1.10

Use the function cbind to create a matrix containing the vectors fives and pop.millions. Make sure to name this matrix (the name can be whatever you like). What do you notice about this matrix?

1.11

Check the dimensions of your matrix using the dim, nrow and ncol functions.

1.12

Create a data frame called countries.df, containing the vectors countries and pop.millions.

1.13

Next, create a list called my.list, containing the variable c, the vectors fives and countries, and the data frame countries.df.
Note: Remember, unlike data frames, lists can contain objects of different types. However, as discussed in section 6.7 of Book 2: R Coding Fundamentals, the original names of the objects included in the list get discarded, so we need to assign the names we want to each object in our list.
Once you have created your list, use the names R function to check the name of each object in the list, to verify your code has worked as intended.

1.14

In this computer lab, we have focused on using R to create our own small data sets, using matrices, data frames and lists. However, we will often want to analyse pre-existing data sets, or use specific functions that are not loaded in base R.
In such instances, we will often need to use add-on R packages.
Many R packages are installed alongside R, but need to be loaded into our current session if we would like to use them. One such package is MASS, which we will look at in more detail in Computer Lab 2.
To load a package in R, we can use the library function.
Try loading the MASS package now.

1.15

Some add-on R packages will not have been installed alongside R, and so we will need to download and install them before we can use them.
The majority of add-on R packages can be found on CRAN, the Comprehensive R Archive Network. One such package, which we will use throughout the semester, is the palmerpenguins R package (Horst, Hill, and Gorman 2020). This package contains a data set on penguins living in the Antarctic.
To install an R package from CRAN, we use the function install.packages. Run the code below to install the palmerpenguins package now.

install.packages("palmerpenguins")

Once the package is installed, we can now load it into our current session. Load the palmerpenguins package now, following the same process you used in 1.14.

1.16

Use what you have learnt in 1.5 to take a look at the help file for the palmerpenguins package.

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值