修改现有数据集

时间:2012-02-19 18:20:04

标签: r

假设我们有一个100乘100的数字数据集。我们想通过取所有数字的对数来改变它。在R中,你会怎么做?

1 个答案:

答案 0 :(得分:11)

首先,让我们做一些示例数据。我制作了一个100 x 100的矩阵,里面填充了正的随机数。

## Save the fake data into the object called "Data"
> Data <- matrix(abs(rnorm(10000)),100,100)

## We can confirm the dimensions of the matrix like so
> dim(Data)
[1] 100 100

## We can confirm that it is a matrix like so
> class(Data)
[1] "matrix"

## We can take a peak at rows 1 to 5 and columns 1 to 2 like so
> Data[1:5,1:2]
          [,1]        [,2]
[1,] 1.5814281 0.216556739
[2,] 0.8939682 0.007296336
[3,] 1.7937537 0.955205600
[4,] 0.4994752 1.982777723
[5,] 1.3459607 1.328990348

现在让我们取这些数字的对数并将其保存为新对象。

## First we can take the natural log and save it in the object "Natural"
Natural <- log(Data)

## Or we can take the log base 10 and save it in the object "Base10"
Base10 <- log10(Data)

## To see all of the objects in your working memory, we can type the following
ls()

希望这有帮助!如果您需要有关R基础知识的更多帮助,请尝试以下网站:

  1. http://www.r-tutor.com/
  2. http://twitter.com/#!/RLangTip