R语言数据结构4—dataframe

dataframe is about datasets containing different data types instead of only one.由于不同的列可以包含不同模式(数值型、字符型等)的数据,数据框的概念较矩阵来说更为一般。由于数据有多种模式,无法将此数据集放入一个矩阵。在这种情况下,使用数据框是最佳选择。


#head,tail分别显示mtcars的前几行和后几行,使我们对数据有个大概的了解

head(mtcars)

tail(mtcars)


str(mtcars)

  • For a data frame it tells you:

    • The total number of observations (e.g. 32 car types)

    • The total number of variables (e.g. 11 car features)

    • A full list of the variables names (e.g. mpg, cyl ... )

    • The data type of each variable (e.g. num for car features)

    • The first observations

#创建一个dataframe

mydataframe <- dataframe(col1,col2,col3,....)

planets <- c("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus",

   "Neptune")

type <- c("Terrestrial planet", "Terrestrial planet", "Terrestrial planet",

   "Terrestrial planet", "Gass giant", "Gass giant", "Gass giant", "Gass giant")

diameter <- c(0.382, 0.949, 1, 0.532, 11.209, 9.449, 4.007, 3.883)

rotation <- c(58.64, -243.02, 1, 1.03, 0.41, 0.43, -0.72, 0.67)

rings <- c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)



# Create the data frame:

planets.df <- data.frame(planets,type,diameter,rotation,rings)

planets.df

> planets.df

planets               type diameter rotation rings
1 Mercury Terrestrial planet    0.382    58.64 FALSE
2   Venus Terrestrial planet    0.949  -243.02 FALSE
3   Earth Terrestrial planet    1.000     1.00 FALSE
4    Mars Terrestrial planet    0.532     1.03 FALSE
5 Jupiter         Gass giant   11.209     0.41  TRUE
6  Saturn         Gass giant    9.449     0.43  TRUE
7  Uranus         Gass giant    4.007    -0.72  TRUE
8 Neptune         Gass giant    3.883     0.67  TRUE

> str(planets.df)

'data.frame':8 obs. of  5 variables:
$ planets : Factor w/ 8 levels "Earth","Jupiter",..: 4 8 1 3 2 6 7 5
$ type    : Factor w/ 2 levels "Gass giant","Terrestrial planet": 2 2 2 2 1 1 1 1
$ diameter: num  0.382 0.949 1 0.532 11.209 ...
$ rotation: num  58.64 -243.02 1 1.03 0.41 ...
$ rings   : logi  FALSE FALSE FALSE FALSE TRUE TRUE ...


#只选择一个属性

furthest.planets.diameter <- planets.df[3:8,"diameter"]

furthest.planets.diameter

> furthest.planets.diameter

[1]  1.000  0.532 11.209  9.449  4.007  3.883


#完整显示数据防止被截断,使用$符号

rings.vector <- planets.df$rings

rings.vector

> rings.vector

[1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE


# Select the information on planets with rings:

rings.vector <- planets.df$rings

planets.with.rings.df <- planets.df[rings.vector,]

planets.with.rings.df


> planets.with.rings.df

      type planets diameter rotation rings
5 Gass giant Jupiter   11.209     0.41  TRUE
6 Gass giant  Saturn    9.449     0.43  TRUE
7 Gass giant  Uranus    4.007    -0.72  TRUE
8 Gass giant Neptune    3.883     0.67  TRUE


#它和下面的语句是等价的

subset(planets.df, subset=(planets.df$rings == TRUE))


order() is a function that, when applied on a variable, gives you in return the position of each element. Let's look at the vector a: a <- c(100,9,101). Now order(a)returns 2,1,3.

a[order(a)]返回排列好后的a


positions <-order(planets.df$diameter,decreasing=TRUE)

# Create new 'ordered' data frame:

largest.first.df <- planets.df[positions,]

# Show me the

largest.first.df

> largest.first.df

              type planets diameter rotation rings
5         Gass giant Jupiter   11.209     0.41  TRUE
6         Gass giant  Saturn    9.449     0.43  TRUE
7         Gass giant  Uranus    4.007    -0.72  TRUE
8         Gass giant Neptune    3.883     0.67  TRUE
3 Terrestrial planet   Earth    1.000     1.00 FALSE
2 Terrestrial planet   Venus    0.949  -243.02 FALSE
4 Terrestrial planet    Mars    0.532     1.03 FALSE
1 Terrestrial planet Mercury    0.382    58.64 FALSE


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值