Consider this simple vector:
x <- c(1,2,3,4,5)
\Sexpr{x} will print in LaTeX 1,2,3,4,5 but I often I need to report some vectors in text as a human, including "and" before the last number.
I tried todo automatically with this function:
x <- c(1,2,3,4,5)
nicevector <- function(x){
a <- head(x,length(x)-1)
b <- tail(x,1)
cat(a,sep=", ");cat(" and ");cat(b)}
nicevector(x)
That seem to work in the console \Sexpr{nicevector(x)} but failed miserably in the .Rnw file (while \Sexpr{x} works). Some ideas?
You can use knitr::combine_words(x).
Using cat() is only for its side-effect: printing in the console. cat() won't return a character string, so you won't see anything in the output. By comparison, knitr::combine_words() returns a character string.
There is also a function for this in glue package
glue::glue_collapse(1:4, ",", last = " and ")
#> 1, 2, 3 and 4
See help of function
Related
I am trying to understand names, lists and lists of lists in R. It would be convenient to have a way to dynamically label them like this:
> ll <- list("1" = 2)
> ll
$`1`
[1] 2
But this is not working:
> ll <- list(as.character(1) = 2)
Error: unexpected '=' in "ll <- list(as.character(1) ="
Neither is this:
> ll <- list(paste(1) = 2)
Error: unexpected '=' in "ll <- list(paste(1) ="
Why is that? Both paste() and as.character() are returning "1".
The reason is that paste(1) is a function call that evaluates to a string, not a string itself.
The The R Language Definition says this:
Each argument can be tagged (tag=expr), or just be a simple expression.
It can also be empty or it can be one of the special tokens ‘...’, ‘..2’, etc.
A tag can be an identifier or a text string.
Thus, tags can't be expressions.
However, if you want to set names (which are just an attribute), you can do so with structure, eg
> structure(1:5, names=LETTERS[1:5])
A B C D E
1 2 3 4 5
Here, LETTERS[1:5] is most definitely an expression.
If your goal is simply to use integers as names (as in the question title), you can type them in with backticks or single- or double-quotes (as the OP already knows). They are converted to characters, since all names are characters in R.
I can't offer a deep technical explanation for why your later code fails beyond "the left-hand side of = is not evaluated in that context (of enumerating items in a list)". Here's one workaround:
mylist <- list()
mylist[[paste("a")]] <- 2
mylist[[paste("b")]] <- 3
mylist[[paste("c")]] <- matrix(1:4,ncol=2)
mylist[[paste("d")]] <- mean
And here's another:
library(data.table)
tmp <- rbindlist(list(
list(paste("a"), list(2)),
list(paste("b"), list(3)),
list(paste("c"), list(matrix(1:4,ncol=2))),
list(paste("d"), list(mean))
))
res <- setNames(tmp$V2,tmp$V1)
identical(mylist,res) # TRUE
The drawbacks of each approach are pretty serious, I think. On the other hand, I've never found myself in need of richer naming syntax.
Tracemem is doing what I need it to, but it is also producing distracting visual clutter. Here is a simple example.
a<-1
b<-2
dummyfunction<-function(x,y){return(sum(x,y))}
dummyfunction(a,b)
[1] 3
Now, I want to do something more complex, first tracemem to see if the inputs are duplicated...
dummyfunction2<-function(x,y){if (tracemem(x)==tracemem(y)){return("Input vectors are identical")}
if(sum(x %in% y)>=length(x) & sum(y %in% x)>=length(y)){print("Something something.")}
return(sum(x,y))}
This does what I want if the inputs are duplicated...
dummyfunction2(a,a)
[1] "Input vectors are identical"
When they're not duplicated, though the function still works, it spews a bunch of confusing information.
dummyfunction2(a,b)
tracemem[0x0000000009824470 -> 0x000000000a7ced80]: match %in% dummyfunction2
tracemem[0x0000000009824500 -> 0x000000000a7cedb0]: match %in% dummyfunction2
tracemem[0x0000000009824500 -> 0x000000000a7cef90]: match %in% dummyfunction2
tracemem[0x0000000009824470 -> 0x000000000a7cc1a8]: match %in% dummyfunction2
[1] 3
I'm hoping to convince non-R users to try using a function with this issue, and output like this will certainly scare them off.
What is the most elegent way to remove this visual clutter without supressing potentially informative warnings. etc that may crop up in other portions of the function?
From http://stat.ethz.ch/R-manual/R-patched/library/base/html/tracemem.html :
"This function marks an object so that a message is printed whenever the internal code copies the object."
You could stick untracemem into the function to get around it:
dummyfunction3<-function(x,y){
if (tracemem(x)==tracemem(y)){return("Input vectors are identical")}
untracemem(x)
untracemem(y)
if(sum(x %in% y)>=length(x) & sum(y %in% x)>=length(y)){print("Something something.")}
return(sum(x,y))}
output:
a <- 1
b <- 2
dummyfunction3(a,a)
# [1] "Input vectors are identical"
dummyfunction3(a,b)
# [1] 3
Don't use tracemem(). Instead you could try pryr::address() which
just returns the memory address of the input.
devtools::install_github("hadley/pryr")
library(pryr)
x <- 1:10
y <- x
address(x)
## [1] "0x100a568c8"
address(y)
## [1] "0x100a568c8"
I want to use information from a field and include it in a R function, e.g.:
data #name of the data.frame with only one raw
"(if(nclusters>0){OptmizationInputs[3,3]*beta[1]}else{0})" # this is the raw
If I want to use this information inside a function how could I do it?
Another example:
A=c('x^2')
B=function (x) A
B(2)
"x^2" # this is the return. I would like to have the return something like 2^2=4.
Use body<- and parse
A <- 'x^2'
B <- function(x) {}
body(B) <- parse(text = A)
B(3)
## [1] 9
There are more ideas here
Another option using plyr:
A <- 'x^2'
library(plyr)
body(B) <- as.quoted(A)[[1]]
> B(5)
[1] 25
A <- "x^2"; x <- 2
BB <- function(z){ print( as.expression(do.call("substitute",
list( parse(text=A)[[1]], list(x=eval(x) ) )))[[1]] );
cat( "is equal to ", eval(parse(text=A)))
}
BB(2)
#2^2
#is equal to 4
Managing expressions in R is very weird. substitute refuses to evaluate its first argument so you need to use do.call to allow the evaluation to occur before the substitution. Furthermore the printed representation of the expressions hides their underlying representation. Try removing the fairly cryptic (to my way of thinking) [[1]] after the as.expression(.) result.
I am trying to create a vector of character strings in R using a loop, but am having some trouble. I'd appreciate any help anyone can offer.
The code I'm working with is a bit more detailed, but I've tried to code a reproducible example here which captures all the key bits:
vector1<-c(1,2,3,4,5,6,7,8,9,10)
vector2<-c(1,2,3,4,5,6,7,8,9,10)
thing<-character(10)
for(i in 1:10) {
line1<-vector1[i]
line2<-vector2[i]
thing[i]<-cat(line1,line2,sep="\n")
}
R then prints out the following:
1
1
Error in thing[i] <- cat(line1, line2, sep = "\n") :
replacement has length zero
What I'm trying to achieve is a character vector where each character is split over two lines, such that thing[1] is
1
1
and thing[2] is
2
2
and so on. Does anyone know how I could do this?
cat prints to the screen, but it returns NULL- to concatenate to a new character vector, you need to use paste:
thing[i]<-paste(line1,line2,sep="\n")
For example in an interactive terminal:
> line1 = "hello"
> line2 = "world"
> paste(line1,line2,sep="\n")
[1] "hello\nworld"
> ret <- cat(line1,line2,sep="\n")
hello
world
> ret
NULL
Though note that in your case, the entire for loop could just be replaced with the more concise and efficient line:
thing <- paste(vector1, vector2, sep="\n")
# [1] "1\n1" "2\n2" "3\n3" "4\n4" "5\n5" "6\n6" "7\n7" "8\n8"
# [9] "9\n9" "10\n10"
I want to replace non-ascii characters (for now, only spanish), by their ascii equivalent. If I have "á", I want to replace it with "a" and so on.
I built this function (works fine), but I don't want to use a loop (including internal loops like sapply).
latin2ascii<-function(x) {
if(!is.character(x)) stop ("input must be a character object")
require(stringr)
mapL<-c("á","é","í","ó","ú","Á","É","Í","Ó","Ú","ñ","Ñ","ü","Ü")
mapA<-c("a","e","i","o","u","A","E","I","O","U","n","N","u","U")
for(y in 1:length(mapL)) {
x<-str_replace_all(x,mapL[y],mapA[y])
}
x
}
Is there an elegante way to solve it? Any help, suggestion or modification is appreciated
gsubfn() in the package of the same name is really nice for this sort of thing:
library(gsubfn)
# Create a named list, in which:
# - the names are the strings to be looked up
# - the values are the replacement strings
mapL <- c("á","é","í","ó","ú","Á","É","Í","Ó","Ú","ñ","Ñ","ü","Ü")
mapA <- c("a","e","i","o","u","A","E","I","O","U","n","N","u","U")
# ll <- setNames(as.list(mapA), mapL) # An alternative to the 2 lines below
ll <- as.list(mapA)
names(ll) <- mapL
# Try it out
string <- "ÍÓáÚ"
gsubfn("[áéíóúÁÉÍÓÚñÑüÜ]", ll, string)
# [1] "IOaU"
Edit:
G. Grothendieck points out that base R also has a function for this:
A <- paste(mapA, collapse="")
L <- paste(mapL, collapse="")
chartr(L, A, "ÍÓáÚ")
# [1] "IOaU"
I like the version by Josh, but I thought I might add another 'vectorized' solution. It returns a vector of unaccented strings. It also only relies on the base functions.
x=c('íÁuÚ','uíÚÁ')
mapL<-c("á","é","í","ó","ú","Á","É","Í","Ó","Ú","ñ","Ñ","ü","Ü")
mapA<-c("a","e","i","o","u","A","E","I","O","U","n","N","u","U")
split=strsplit(x,split='')
m=lapply(split,match,mapL)
mapply(function(split,m) paste(ifelse(is.na(m),split,mapA[m]),collapse='') , split, m)
# "iAuU" "uiUA"