Remove last n rows or column or specified elements from a data frame like array_pop in PHP
Source:R/data_pop.R
data_pop.Rd
Shorthand to remove elements from a data frame and save as the same name
Usage
data_pop(., n = 1, which = c("rows", "cols"), ret = FALSE)
Arguments
- .
parent data
- n
number of elements to remove
- which
whether to remove from row or from column
- ret
TRUE or FALSE. whether to return value instead of setting it to the parent data
Examples
data.01 <- mtcars[1:7,]
#task: remove 1 element from the end of the data and set it to the data name
data.01 #data.01 data before pop
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
#> Duster 360 14.3 8 360 245 3.21 3.570 15.84 0 0 3 4
data_pop(data.01) #does not return anything
data.01 #data.01 data updated after pop
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
#task: remove 3 columns from the end of the data and set it to the data name
data.01 #data.01 data before pop
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
data_pop(data.01, n = 3, which = "cols") #does not return anything, but updates data
data.01 #data.01 data updated after pop
#> mpg cyl disp hp drat wt qsec vs
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1
#task: remove 5 elements from the end, but do not set it to the data name
data.01 #data.01 data before pop
#> mpg cyl disp hp drat wt qsec vs
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1
data_pop(data.01,5, ret = TRUE) #return modified data
#> mpg cyl disp hp drat wt qsec vs
#> Mazda RX4 21 6 160 110 3.9 2.62 16.46 0
data.01 #data.01 data remains the same after pop
#> mpg cyl disp hp drat wt qsec vs
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1