This question already has answers here:
Create integer sequences defined by 'from' and 'to' vectors
(2 answers)
Closed 5 years ago.
I have two vectors c(1,2) and c(9,10)
and I want have outputs as follows:
c(1,2,3,4,5,6,7,8,9) and (2,3,4,5,6,7,8,9,10)
it seems simple but I can't figure it out...
thanks~
We can use Map to create a list of vectors. Here, the : will get the sequence of values from each corresponding elements of 'v1' and 'v2'
Map(`:`, v1, v2)
data
v1 <- 1:2
v2 <- 9:10
Related
This question already has answers here:
How to cbind or rbind different lengths vectors without repeating the elements of the shorter vectors?
(6 answers)
How to convert a list consisting of vector of different lengths to a usable data frame in R?
(6 answers)
Closed last month.
I was wondering if there is an efficient way to convert each element (each with different length) in my List to a column in a data frame to achieve my Desired_output?
I tried the following without success:
dplyr::bind_cols(List)
Note: This data is toy, a functional answer is appreciated.
List <- list(`1000`=letters[1:2], `2000`=letters[1:3], `3000`=letters[1:4])
Desired_output <-
data.frame(`1000`= c(letters[1:2],"",""),
`2000`= c(letters[1:3],""),
`3000`= letters[1:4])
You could try this.
library(purrr)
l <- list(`1000`=letters[1:2], `2000`=letters[1:3], `3000`=letters[1:4])
# get max lengths across all list items
max_len = max(lengths(l))
# using purrr::modify add as many empty characters needed until each
# item has the same number of rows as the item with the most
l = modify(l, function(f) {
f = c(f, rep("", max_len-length(f)))
})
as.data.frame(l)
X1000 X2000 X3000
1 a a a
2 b b b
3 c c
4 d
This question already has answers here:
R: Count number of objects in list [closed]
(5 answers)
Closed 2 years ago.
I have a dataframe in R, and I am trying to set all cells in the form of a vector, either c(1,2,3) or 1:2 to NA. Is there any easy way to do this?
You can use lengths to count number of elements in each value of column. Set them to NA where the length is greater than 1. Here I am considering dataframe name as df and column name as col_name. Change them according to your data.
df$col_name[lengths(df$col_name) > 1] <- NA
This question already has answers here:
How to generate all possible combinations of vectors without caring for order?
(2 answers)
Closed 3 years ago.
I have multiple character strings, let's say 'pred_1', 'pred_2' and 'pred_3'. Now I want to get a list with all pairs of the strings. So, the resulting list should contain 'pred_1 pred_2', 'pred_1 pred_3' and 'pred_2 pred_3'. Does anyone know how to automate this for more than three character strings?
An option is combn
combn(v1, 2, simplify = FALSE)
data
v1 <- paste0("pred_", 1:3)
This question already has answers here:
How can I generate all the possible combinations of a vector
(2 answers)
Compute all pairwise differences of elements in a vector [duplicate]
(1 answer)
Closed 5 years ago.
I am interested to generate all the possible combinations of a vector, for example,
v1 <- c("A", "Aa", "B")
and all the possible combinations will be
"A-Aa" "Aa-A" "B-A" "B-Aa" "A-B" "Aa-B"
I saw a post- How can I generate all the possible combinations of a vector, but it does not give correct results for v1. The same thing happens to v1 <- c("Testis_NOA_ID","Testis_NOA_IDSt") which returns "Testis_NOA_ID-Testis_NOA_IDSt" only but I am expecting "Testis_NOA_IDSt-Testis_NOA_ID" also.
I checked some other vectors such as v1 <- c("a,"b","c) or v1 <- c("normal", "cancer"), which it gives correct results. The problem comes when the vector content is repeating like v1 <- c("Testis_NOA_ID","Testis_NOA_IDSt"). How to fix it.
This question already has answers here:
grep using a character vector with multiple patterns
(11 answers)
Find matching strings between two vectors in R
(2 answers)
Closed 5 years ago.
I have a two different vectors
v1 <- c("HelloWorld", "Climate","fooboo","testtesting")
v2 <- c("hello","test")
I want to compare both the vector and need to get "HelloWorld" and "testingtestin".
I used regexpr, but i have'nt get the result.
sapply(v1 , regexpr, v2 , ignore.case=TRUE)