This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to remove rows of a matrix by row name, rather than numerical index?
removing elements in one vector from another in R
I have two vectors:
a<-c(1,2,3,4,5,6,7,8)
b<-c(7,3,6,4,8,1)
I would like to select those elements of a which are not in b
I tried subset(a, a!=b) but I get the warning:
longer object length is not a multiple of shorter object length
Try setdiff for vectors:
R> setdiff(a,b)
[1] 2 5
Try this:
a[!(a%in%b)]
Look at ?"%in%".
Related
This question already has answers here:
What does the diff() function in R do? [closed]
(2 answers)
Closed 5 months ago.
I dont know what´s the posible way of creating a new vector from a previous one by subtraction
of the second element to the first one and doing it for all the values of the vector, by
applying Xi-X(i-1)
Use diff
> x <- c(5,2,3,4)
> diff(x)
[1] -3 1 1
This question already has answers here:
Count number of distinct values in a vector
(6 answers)
Closed 1 year ago.
I am stuck on a question in R, i need to sum a list of characters from the starwars dataset, which is the starwars eye color. The question asks how many different eye colours the character have. The answer is 15, which i derived from a table (table(starwars$eyecolor), but i cannot figure the code to get to 15.
Try any of below
> length(levels(factor(starwars$eye_color)))
[1] 15
> length(unique(starwars$eye_color))
[1] 15
> sum(!duplicated(starwars$eye_color))
[1] 15
length(unique(starwars$eye_color))
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:
do.call(rbind, list) for uneven number of column
(4 answers)
Closed 9 years ago.
There are objects with basic datas like a<-list(a=1,b="A",c=character())
Now I want convert it to a data.frame, but there for I need equal rows. How to fill the empty vectors with NA in easy way to run as.data.frame(a)? the only Idea I have is to ask if one elment of the list has length<1 then set element[1]=NA.
I'm not sure this is any cleaner, but it does get rid of the if stuff:
lfoo<-list(one=1:3,two=character(),three=4:6,four=vector())
dfoo<-dfoo<-data.frame(one=rep(NA,3),two=rep(NA,3), three=rep(NA,3),four=rep(NA,3))
lvalues <- which(unlist(lapply(1:4,function(x) length(lfoo[[x]]) > 0))
for (j in lvalues) dfoo[,jvalues]<-lfoo[[jvalues]]
This may point you to simple ways of dealing with conversions and selective replacements.