Generate all the possible combinations of a vector [duplicate] - r

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.

Related

is there a way to set all cells in a dataframe in the form of a vector as NA? [duplicate]

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

How to get list of all combinations of pairs of character strings in R [duplicate]

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)

Two vectors sequence one to another [duplicate]

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

How to check if a vector is a part other vector in R [duplicate]

This question already has answers here:
R, whether all the elements of X are present in Y
(3 answers)
Closed 6 years ago.
I have two vectors x<-c("A","B") and y<-c("A", "B", "C") I want to find if whole of x is contained in y. Also, the order does not matter, both elements in x should be a part of y.
Thanks!
You can check with:
sum(!x %in% y)==0
EDIT:
Or as suggested, you can achieve something more readable with
all(x %in% y)

Deleting Rows in R [duplicate]

This question already has answers here:
Subset dataframe by multiple logical conditions of rows to remove
(8 answers)
Closed 7 years ago.
I'm trying to delete all rows in a dataframe when the average of a vector > an individual number in the vector. For some reason it seems to pick and choose which ones it deletes. All help is appreciated thank you, here is my code.
k<-c(HW2$AGE)
j<-mean(k)
for (i in HW2$AGE)
if (j>i){
HW2 <- HW2[-i, ]
}
Don't need to vectorise. Instead I would use the below
Sample data
x <- data.frame("A"= runif(10), "B" = runif(10))
Calculate mean
xMean <- mean(x[,"A"])
Exclude rows
y <- x[x$A < xMean,]
This is probably the most obvious way of excluding unwanted rows

Resources