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
Related
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:
Replace given value in vector
(8 answers)
Closed 4 years ago.
hi just learning R and looking at how to do this.
How do i change a value of "1" in vehicle vector where it should be filled with the proper value from vendor$shop vector. I'm trying to make a weighted adjacency matrix
vehicle_vector[vehicle_vector == 1] <- new_value
This question already has answers here:
Getting the last n elements of a vector. Is there a better way than using the length() function?
(6 answers)
Closed 5 years ago.
suppose I have daily time series data under variable name "prices", but im only interested in the past 100 days. How would i extract the last 100 elements from this variable?
Something equivalent to python's prices[-100:] but for R?
If it's a vector:
tail(prices, 100)
This question already has answers here:
How to randomize a vector
(2 answers)
Closed 6 years ago.
Supposing I have the vector
x <- c(1,2,3,4,5,6)
Is there any way I could randomly mix its elements?
Or create a vector which would have integer elements from 1 to 6 which do not repeat?
We need sample to do that
sample(x)
If it needs to be repeated, use the replicated
replicate(3, sample(x))
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%".