The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.
The examples below use a variety of infix functions that can be used to change values in a vector.
If you already know indices of the columns or rows you want to change, these functions will not be shorter, however often the opposite is the case, one knows a name, but not the exact column/row number. In these cases the infix functions are much shorter.
<- data.frame(x=1:5, y=6, txt=paste0("delta = ",6-1:5), row.names=paste0("ri",5:1), stringsAsFactors = FALSE)
d ::kable(d) knitr
x | y | txt | |
---|---|---|---|
ri5 | 1 | 6 | delta = 5 |
ri4 | 2 | 6 | delta = 4 |
ri3 | 3 | 6 | delta = 3 |
ri2 | 4 | 6 | delta = 2 |
ri1 | 5 | 6 | delta = 1 |
# Change a column name based on the current name
# colnames(d)[colnames(d)%in%"y"] <- "Yhat"
colnames(d)["y"%ci%d] <- "Yhat"
1:3%ci%d
> [1] "x" "Yhat" "txt"
# Use a range of values in variable to change cells
# d$txt[d$x>=2&d$x<=4] <- "Changed!"
$txt[d$x%[]%c(2,4)] <- "Changed!"
d::kable(d) knitr
x | Yhat | txt | |
---|---|---|---|
ri5 | 1 | 6 | delta = 5 |
ri4 | 2 | 6 | Changed! |
ri3 | 3 | 6 | Changed! |
ri2 | 4 | 6 | Changed! |
ri1 | 5 | 6 | delta = 1 |
# Use row names to change cells
# d$txt[rownames(d)%in%c("ri2,ri4")] <- "Changed again!"
$txt[c("ri2","ri4")%ri%d] <- "Changed again!"
d::kable(d) knitr
x | Yhat | txt | |
---|---|---|---|
ri5 | 1 | 6 | delta = 5 |
ri4 | 2 | 6 | Changed again! |
ri3 | 3 | 6 | Changed! |
ri2 | 4 | 6 | Changed again! |
ri1 | 5 | 6 | delta = 1 |
Use the rose tinted glasses %00%
to change any nasty
number into a cute fluffy value of your choice.
# Replace special numerical values
c(0,Inf,1,NA,2,NaN,3) %00% NA
> [1] 0 NA 1 NA 2 NA 3
# Also works with NULL
NULL %00% NA
> [1] NA
# Length 0 vectors
logical(0) %00% NA
> [1] NA
numeric(0) %00% "lenghth 0!!"
> [1] "lenghth 0!!"
0 %00% "lenghth 0!!"
> [1] 0
The counter infix functions can add or subtract values from an input source. This can be a value, or an object.
# Signed increment
# Notice the difference between passing an object and a value for counter
# Value
11 %+-% -5)
(> [1] 6
11 %+-% 5)
(> [1] 16
# Object
<- 11
i %+-% -5)
(i > [1] 6
%+-% 5)
(i > [1] 11
# This means we can use the infix in a while ... statement
# WARNING: As is the case for any while ... statement, be careful not to create an infinite loop!
<- 5
i while(i > -5){
%+-% -1
i print(i)
}> [1] 4
> [1] 3
> [1] 2
> [1] 1
> Warning in i %+-% -1: Positive valued counter changed sign (counter <= 0)!
> [1] 0
> [1] -1
> [1] -2
> [1] -3
> [1] -4
> [1] -5
# Non-negative increment
# Notice the difference between passing an object and a value for counter
# Value
0 %++% 5)
(> [1] 5
0 %++% 5)
(> [1] 5
# Object
<- 0
i %++% 5)
(i > [1] 5
%++% 5)
(i > [1] 10
# This means we can use the infix in a while ... statement
# WARNING: As is the case for any while ... statement, be careful not to create an infinite loop!
<- 0
i while(i < 20){
%++% 5
i print(i)
}> [1] 5
> [1] 10
> [1] 15
> [1] 20
Use x %[+% n
to add n
0
s to
the front of x
. Similarly, %+]%
would add to
the rear, and %[+]%
to front and rear. If you want to add a
value other than 0
, use x %[+% c(n,v)
set.seed(1234)
<- round(runif(10,1,10)))
(x > [1] 2 7 6 7 9 7 1 3 7 6
# Pad front with 10 zeros
%[+%5
x> [1] 0 0 0 0 0 2 7 6 7 9 7 1 3 7 6
# Same as
%[+% c(5,0)
x> [1] 0 0 0 0 0 2 7 6 7 9 7 1 3 7 6
# Pad rear with zeros
%+]%5
x> [1] 2 7 6 7 9 7 1 3 7 6 0 0 0 0 0
# Same as
%+]%c(5,0)
x> [1] 2 7 6 7 9 7 1 3 7 6 0 0 0 0 0
# Pad front + rear with NA
%[+]%c(4,NA)
x> [1] NA NA 2 7 6 7 9 7 1 3 7 6 NA NA
# Pad front + rear of a character vector
"yes"%[+]%c(2,"no")
> [1] "no" "yes" "no"
"yes"%[+]%c(1,"no")
> [1] "yes" "no"
"yes"%[+]%c(0,"no")
> [1] "yes"
Use x %[-% n
, x %-]% n
, or
x %[-]% c(n,m)
to trim x
by n
from the front, rear, or, n
from the front and
m
fro the rear. When n
is uneven,
floor(n)
wil be trimmed from the front and
ceiling(n)
from the rear.
set.seed(4321)
<- round(runif(10,1,10)))
(x > [1] 4 9 5 1 8 8 8 9 5 5
# Trim front
%[-%5
x> [1] 8 8 9 5 5
# Trim rear
%-]%5
x> [1] 4 9 5 1 8
# Trim front + rear
%[-]%c(2,4)
x> [1] 5 1 8 8
%[-]%3
x> [1] 9 5 1 8 8 8 9
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.