How can I join a vector with commas [duplicate] - r

This question already has answers here:
How can I write a comma between each value of a vector?
(2 answers)
Closed 9 years ago.
b = c(1,1,1,1)
[1] 1 1 1 1
What can I do to change b into "1,1,1,1"

Use paste0 setting collapse=","
> paste0(b, collapse=",")
[1] "1,1,1,1"
Note that the result is not longer numeric, it's character.
cat is another alternative
> cat(b, sep=",")
1,1,1,1
as pointed out by #digEmAll, you can not store the output of cat since it only prints the output on the console, if you want to capture that output to store it in an object, then one alternative is using capture.output as in:
capture.output (cat(b, sep=","))
which is the same result provided by paste0 above.

Related

How do I get the number of numbers in a text in R? [duplicate]

This question already has answers here:
count number of digits in a string in r
(2 answers)
Closed 3 years ago.
I want to know how many digits do I have in a text variable. For example, a function that in the text "ABC234" the answer would be 3.
I tried with this:
aa=gregexpr("[[:digit:]]+\\.*[[:digit:]]*","ABC234")
I almost have it, but honestly I still dont understand the lists, so I have no idea how to get it.
Any function? Or how to manage it with my almost-option?
Thanks
Match each digit and then take the length of the returned value:
lengths(gregexpr("\\d", "ABC234"))
## [1] 3
or replace each non-digit with a zero length string and take the length of what remains:
nchar(gsub("\\D", "", "ABC234"))
## [1] 3
As an option you can use stringi or stringr libraries as well:
stringi::stri_count('ABC234', regex = '\\d')
# [1] 3
stringr::str_count('ABC234', '\\d')
# [1] 3
You can use the dpylr and readr package as follows:
library(readr)
library(dplyr)
string = "ABC234"
parse_number(string) %>%
nchar()
[1] 3

Remove characters in string before specific symbol(including it) [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Use gsub remove all string before first white space in R
(4 answers)
Closed 5 years ago.
at the beginning, yes - simillar questions are present here, however the solution doesn't work as it should - at least for me.
I'd like to remove all characters, letters and numbers with any combination before first semicolon, and also remove it too.
So we have some strings:
x <- "1;ABC;GEF2"
y <- "X;EER;3DR"
Let's do so gsub() with . and * which means any symbol with occurance 0 or more:
gsub(".*;", "", x)
gsub(".*;", "", y)
And as a result i get:
[1] "GEF2"
[1] "3DR"
But I'd like to have:
[1] "ABC;GEF2"
[1] "EER;3DR"
Why did it 'catch' second occurence of semicolon instead of first?
You could use
gsub("[^;]*;(.*)", "\\1", x)
# [1] "ABC;GEF2"

Cutting value in vector by determine positions [duplicate]

This question already has answers here:
Trying to return a specified number of characters from a gene sequence in R
(3 answers)
Extracting the last n characters from a string in R
(15 answers)
Closed 5 years ago.
Is there a function in R that I can cut a value in vector.
for example i got this vec:
40754831597
64278107602
64212163451
and each vale in the vec i want to cut so from the number pos 3 to 6 for example and get a new vector look like this
7548
2781
2121
and so on
I don't really get why you would like to do this, but here you go:
# assuming it's a character vector
substring(vec,3,6)
# if it's numeric
substring(as.character(vec),3,6)
#output
#[1] "7548" "2781" "2121"
We can use sub
sub(".{2}(.{4}).*", "\\1", v1)
#[1] "7548" "2781" "2121"
data
v1 <- c(40754831597, 64278107602, 64212163451)

Finding strings in columns of dataframe in R [duplicate]

This question already has answers here:
R grep: is there an AND operator?
(4 answers)
Closed 8 years ago.
So if I have the following
list <- c("catdog","chicken","poop")
names <- c("Fabio","John","Jack")
df <- data.frame(names, list, stringsAsFactors=FALSE)
names list
1 Fabio catdog
2 John cat
3 Jack dog
Assuming list is a column of strings. I want to know how can I return rows where "cat" AND "dog" after appearing once as a pair they may appear more times. I tried:
want <- c("cat","dog")
df[grepl(paste(want,collapse="&"),df$list),]
I know this works with "|" for some reason its not working with "&". Let me know if anyone can help with this. Thanks!
This is an option, if neither 'cat' nor 'dog' can repeat within a single string.
df[grepl('(cat)|(dog).*(\\1|\\2)', df$list), ]

Capture last output as an R object [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to assign the result of the previous expression to a variable in R?
I'm working with R in ESS and just made the stupid mistake of running a long running function without assigning the result to a variable. So, it just printed out the result, a long string of output that looks like:
[[1]]
1 FALSE
[[2]]
1 TRUE
[[3]]
1 TRUE
[[4]]
1 TRUE
Is there any way to coerce this printed output into an R object? Either within R, or using emacs (M-x undo-my-stupid-mistake)?
Maybe this will work:
out <- .Last.value

Resources