Transform varibles
transform.Rd
Mutate input variables using a formula.
Arguments
- from
a data.frame object with variables
- formula
a formula indicating the operation to create new varibles. Look at the detail section for explanantion.
- as
a character vector with names of new variables.
- na.remove
a logical value indicating whether NA values should be removed.
- logic_convert
logical value indicating if the new logical varaible are converted to
0
or1
- ...
further arguments
Details
The formula is composed of two part:
column_names ~ trasformed_variables
the left-hand side are the names of the column to transform, and the right-hand the operations applied to the selected columns, using the I()
function.
For example:
column_names1 + column_names2 ~ I(log(column_names1)) + I(column_names2/100)
the column_names1
is mutated in log(column_names1)
and column_names2
is divided by 100.
If na.remove
is set to TRUE
, variables are mutaded, and then the observation with missing are removed.
Examples
data("airquality")
dt <- airquality
head(transform(from = dt, Ozone ~ I(Ozone-Ozone)))
#> Ozone Solar.R Wind Temp Month Day
#> 1 0 190 7.4 67 5 1
#> 2 0 118 8.0 72 5 2
#> 3 0 149 12.6 74 5 3
#> 4 0 313 11.5 62 5 4
#> 5 NA NA 14.3 56 5 5
#> 6 0 NA 14.9 66 5 6
head(transform(from = dt, Ozone ~ log(Ozone)))
#> Ozone Solar.R Wind Temp Month Day
#> 1 3.713572 190 7.4 67 5 1
#> 2 3.583519 118 8.0 72 5 2
#> 3 2.484907 149 12.6 74 5 3
#> 4 2.890372 313 11.5 62 5 4
#> 5 NA NA 14.3 56 5 5
#> 6 3.332205 NA 14.9 66 5 6
head(transform(from = dt, Ozone ~ I(Ozone>5)))
#> Ozone Solar.R Wind Temp Month Day
#> 1 1 190 7.4 67 5 1
#> 2 1 118 8.0 72 5 2
#> 3 1 149 12.6 74 5 3
#> 4 1 313 11.5 62 5 4
#> 5 NA NA 14.3 56 5 5
#> 6 1 NA 14.9 66 5 6
head(transform(from = dt, Ozone ~ I(Ozone>5), logic_convert = TRUE))
#> Ozone Solar.R Wind Temp Month Day
#> 1 1 190 7.4 67 5 1
#> 2 1 118 8.0 72 5 2
#> 3 1 149 12.6 74 5 3
#> 4 1 313 11.5 62 5 4
#> 5 NA NA 14.3 56 5 5
#> 6 1 NA 14.9 66 5 6
head(transform(from = dt, ~ log()))
#> Ozone Solar.R Wind Temp Month Day
#> 1 3.713572 5.247024 2.001480 4.204693 1.609438 0.0000000
#> 2 3.583519 4.770685 2.079442 4.276666 1.609438 0.6931472
#> 3 2.484907 5.003946 2.533697 4.304065 1.609438 1.0986123
#> 4 2.890372 5.746203 2.442347 4.127134 1.609438 1.3862944
#> 5 NA NA 2.660260 4.025352 1.609438 1.6094379
#> 6 3.332205 NA 2.701361 4.189655 1.609438 1.7917595
head(transform(from = dt, . ~ log()))
#> Ozone Solar.R Wind Temp Month Day
#> 1 3.713572 5.247024 2.001480 4.204693 1.609438 0.0000000
#> 2 3.583519 4.770685 2.079442 4.276666 1.609438 0.6931472
#> 3 2.484907 5.003946 2.533697 4.304065 1.609438 1.0986123
#> 4 2.890372 5.746203 2.442347 4.127134 1.609438 1.3862944
#> 5 NA NA 2.660260 4.025352 1.609438 1.6094379
#> 6 3.332205 NA 2.701361 4.189655 1.609438 1.7917595
head(transform(from = dt, NULL ~ log()))
#> Ozone Solar.R Wind Temp Month Day
#> 1 3.713572 5.247024 2.001480 4.204693 1.609438 0.0000000
#> 2 3.583519 4.770685 2.079442 4.276666 1.609438 0.6931472
#> 3 2.484907 5.003946 2.533697 4.304065 1.609438 1.0986123
#> 4 2.890372 5.746203 2.442347 4.127134 1.609438 1.3862944
#> 5 NA NA 2.660260 4.025352 1.609438 1.6094379
#> 6 3.332205 NA 2.701361 4.189655 1.609438 1.7917595
head(transform(from = dt, Ozone + Day ~ log()))
#> Ozone Solar.R Wind Temp Month Day
#> 1 3.713572 190 7.4 67 5 0.0000000
#> 2 3.583519 118 8.0 72 5 0.6931472
#> 3 2.484907 149 12.6 74 5 1.0986123
#> 4 2.890372 313 11.5 62 5 1.3862944
#> 5 NA NA 14.3 56 5 1.6094379
#> 6 3.332205 NA 14.9 66 5 1.7917595
head(transform(from = dt, Ozone + Day ~ log(Ozone/100) + exp(Day)))
#> Ozone Solar.R Wind Temp Month Day
#> 1 -0.8915981 190 7.4 67 5 2.718282
#> 2 -1.0216512 118 8.0 72 5 7.389056
#> 3 -2.1202635 149 12.6 74 5 20.085537
#> 4 -1.7147984 313 11.5 62 5 54.598150
#> 5 NA NA 14.3 56 5 148.413159
#> 6 -1.2729657 NA 14.9 66 5 403.428793
head(transform(from = dt, Ozone ~ log()))
#> Ozone Solar.R Wind Temp Month Day
#> 1 3.713572 190 7.4 67 5 1
#> 2 3.583519 118 8.0 72 5 2
#> 3 2.484907 149 12.6 74 5 3
#> 4 2.890372 313 11.5 62 5 4
#> 5 NA NA 14.3 56 5 5
#> 6 3.332205 NA 14.9 66 5 6
head(transform(from = dt,Ozone + Wind ~ C(log(1))))
#> Ozone Solar.R Wind Temp Month Day
#> 1 0 190 0 67 5 1
#> 2 0 118 0 72 5 2
#> 3 0 149 0 74 5 3
#> 4 0 313 0 62 5 4
#> 5 0 NA 0 56 5 5
#> 6 0 NA 0 66 5 6
head(transform(from = dt,Ozone + Wind ~ log(Ozone) + C(10)))
#> Ozone Solar.R Wind Temp Month Day
#> 1 3.713572 190 10 67 5 1
#> 2 3.583519 118 10 72 5 2
#> 3 2.484907 149 10 74 5 3
#> 4 2.890372 313 10 62 5 4
#> 5 NA NA 10 56 5 5
#> 6 3.332205 NA 10 66 5 6
head(transform(from = dt, Ozone + Wind~ C(log(Ozone))))
#> Ozone Solar.R Wind Temp Month Day
#> 1 3.713572 190 3.713572 67 5 1
#> 2 3.583519 118 3.583519 72 5 2
#> 3 2.484907 149 2.484907 74 5 3
#> 4 2.890372 313 2.890372 62 5 4
#> 5 NA NA NA 56 5 5
#> 6 3.332205 NA 3.332205 66 5 6
foo <- function(x, a = 100){return(x-x + a)}
head(transform(from = dt, Ozone + Wind ~ foo(a = 100)))
#> Error in foo(Ozone, a = 100): could not find function "foo"
head(transform(from = dt, . ~ foo(a = 100)))
#> Error in foo(Ozone, a = 100): could not find function "foo"
head(transform(from = dt, Ozone + Wind ~ log(log(1))))
#> Ozone Solar.R Wind Temp Month Day
#> 1 -Inf 190 -Inf 67 5 1
#> 2 -Inf 118 -Inf 72 5 2
#> 3 -Inf 149 -Inf 74 5 3
#> 4 -Inf 313 -Inf 62 5 4
#> 5 -Inf NA -Inf 56 5 5
#> 6 -Inf NA -Inf 66 5 6