Convert a Printed Message into a Character Vector

Convert a printed message into a character vector

The default hander for message() sends the result to stderr via cat(). You can capture that with

tc <- textConnection("messages","w")
sink(tc, type="message")
s <- scan("data.txt")
sink(NULL, type="message")
close(tc)

messages
# [1] "Read 5 items"

Transform character with ranges or values into vector of only values in R

One option is eval(parse

unlist(lapply(v1, function(x) eval(parse(text=x))))

Or after the strsplit, get the sequence with :

unlist(lapply(strsplit(v1, ":"), function(x) Reduce(`:`, as.numeric(x))))
#[1] 1 2 3 4

data

v1 <- c("1:3","4")

Convert multiple strings into object references

Here, we can use mget to return the values of the objects in a list and then unlist the list output

unlist(mget(letters), use.names = FALSE)
#[1] 5 6

Also, if we are using eval(parse, it works only for a single element, so loop through the 'letters' and then do eval(parse

unname(sapply(letters, function(x) eval(parse(text = x))))
#[1] 5 6

However, the recommended option would be mget


letters/LETTERS are inbuilt Constants to return the lower/upper case letters. So, it is better not to use identifier names with reserve words or function names

Trouble using `...` with connections

call fails because you can't use ... in that way with this function. If you turn off the sink, you'll see the error message. You'll want to wrap the call to eval with tryCatch and print the error, if one's encountered, and instead of eval + call, use do.call.

getMessage <- function(FUN, ...)
{
FUN <- deparse(substitute(FUN))
tc <- textConnection("messages", "w")
on.exit(close(tc))
sink(tc, type = "message")
tryCatch(do.call(FUN, list(...)), error=function(e) {message(e$message)})
sink(NULL, type = "message")
messages
}

getMessage(scan, "data.txt")

Check whether an element in a character vector can be converted to numeric in R

Perhaps, you can use regex to find if all the values in a column are either an integer or float.

can_convert_to_numeric <- function(x) {
all(grepl('^(?=.)([+-]?([0-9]*)(\\.([0-9]+))?)$', x, perl = TRUE))
}

sapply(df[catCols], can_convert_to_numeric)
# cat1 cat2
#FALSE TRUE

Alternatively, to get values that cannot be converted to numeric we can use grep as :

values_which_cannot_be_numeric <- function(x) {
grep('^(?=.)([+-]?([0-9]*)(\\.([0-9]+))?)$', x, perl = TRUE, invert = TRUE, value = TRUE)
}

lapply(df[catCols], values_which_cannot_be_numeric)

#$cat1
#[1] "some_string"

#$cat2
#character(0)

Regex taken from here.


If you use type.convert you don't have to worry about this at all.

df <- type.convert(df, as.is = TRUE)
str(df)

#'data.frame': 4 obs. of 3 variables:
# $ cat1 : chr "1.12354" "1.4548" "1.9856" "some_string"
# $ cat2 : num 1.46 1.15 1.96 1.32
# $ target: int 0 1 1 0

convert character string into R integer vector

If it is positive integer then:

as.integer(unlist(strsplit("{1,0,0,0,0}", "\\D+"))[-1])

If negative is a possibility:

as.integer(unlist(strsplit("{1,-3,0,0,0}", "[,{}]"))[-1])


Related Topics



Leave a reply



Submit