remove_rows_data_frame -+Table of Cont

Using the built-in data set air quality , fir st load the data and check what var iables it contains:

data


(
air
quality
)

names

(
air
quality
)

Among ways to subset r ows of a data fr ame in S language, ther e ar e two usual appr oaches. Either you delete r ows by extr acting all other r ows of the data fr ame using a vector of logical values, or you r emove these r ows using a vector of negative indices.

Subset by logical indices

Assume that days 5 and 7 in May of the air quality measur ements ar e outlier s and you want to r epeat an analysis without these r ows. You wr ite:

length

(
air
quality
$Day
)

air
quality2
 <- subset

(
air
quality
, !(
Day %in% c

(
5
, 7
)
 & Month
 == 5
)
)

length

(
air
quality2
$Day
)

Subset by negative indices

Similar ly, you can delete specific r ows. In or der to delete lines 2 and 7, you wr ite:

length

(
air
quality
$Day
)

air
quality3
 <- air
quality
[
-c

(
2
, 7
)
, ]
 
length

(
air
quality3
$Day
)

Tr aps for beginner s

Claudia Beleites 2008/01/02

Be car eful with logical ver sus numer ic index vector s:

new

.data

 <- data

[
!outlier
s
, ]
  # logical indices

new

.data

 <- data

[
-outlier
s
, ]
  # numer
ic indices

Using the numer ic for m for a logical index vector will delete the fir st r ow only:

 >
 data

 <- 1
:10

 >
 outlier
s
 <- data


 %in% 3
:7

 >
 new

.data

 <- data

[
!outlier
s
]
  # (desir
ed effect)

 >
 new

.data

 [1]  1  2  8  9 10 

 >
 new

.data

 <- data

[
-outlier
s
]
  # (wr
ong code)

 >
 new

.data

 [1]  2  3  4  5  6  7  8  9 10
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值