R Command for replacing value [duplicate] - r

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

Related

Subtract values from vector [duplicate]

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

Repeating entries in match in R [duplicate]

This question already has answers here:
Find all positions of all matches of one vector of values in second vector
(3 answers)
Finding All Positions for Multiple Elements in a Vector
(7 answers)
Closed 4 years ago.
In the following example, there is repeating entries in the second vector. How do I make R to tell me the position of both appearances of 5, please?
match(5, c(1,2,9,5,3,6,7,4,5))
Use the which function:
which(c(1,2,9,5,3,6,7,4,5) == 5)

how to divided a vector and get the partition num in R? [duplicate]

This question already has answers here:
Dividing a vector into categories
(1 answer)
How to create a categorical variable in R with unequal categoris [closed]
(1 answer)
R if else with for loop [duplicate]
(3 answers)
Closed 5 years ago.
I'm programming in R.I need to divided a vector into x partitions(eg,x=4),and get the partition number of the vector, something like this...
a <- data.frame(x=1:20)
a$numofpartition<-ifelse(a$x<6,1,ifelse(a$x<11,2,ifelse(a$x<16,3,4)))
As the code,I divided a$x into 4 partitions,and get the partition number for each x, any functions in R could do this? Thank you!

How do i take the last n elements from a vector in R? [duplicate]

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)

The most efficient way to calculate numbero of unique values in vector [duplicate]

This question already has answers here:
How to count the number of unique values by group? [duplicate]
(1 answer)
Unique values in each of the columns of a data frame
(9 answers)
Closed 5 years ago.
Is there better way to compute this instead of:
length(unique(vector))
let's assume that we donno what class is the vector.
You can use
library(data.table)
vector <- c(1,2,2,2,3)
uniqueN(vector)

Resources