creating a numeric vector in r [duplicate] - r

This question already has answers here:
Convert a matrix to a 1 dimensional array
(11 answers)
Closed 4 years ago.
I imported a 70104 x 1 matrix with hourly wind speeds into r. I am trying to fit various energy distributions to the data. however, when I try to fir the models, this error comes up "data must be a numeric vector of length greater than 1". How do I rectify this issue?

We can just wrap with c to convert it to a vector
v1 <- c(mat)
Or explicitly use as.vector to strip off the dim attributes
as.vector(mat)
data
set.seed(24)
mat <- matrix(rnorm(70104))

Related

R Function to extract values greater than x [duplicate]

This question already has answers here:
How can i select values from a vector in R using logical operators?
(3 answers)
Closed 1 year ago.
I am running a simulation model and would like to know how to extract all values greater than 6 in this gamma distribution. Thank you!
cost <- 100
n_samp <-1000
gamma<-rgamma(n_samp,2,0.5)
You can also subset the array gamma with a logical vector:
gt6_values = gamma[gamma > 6]
You can use subset from base R to get just the values greater than 6.
subset(gamma, gamma > 6)

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

R Command for replacing value [duplicate]

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

Cutting value in vector by determine positions [duplicate]

This question already has answers here:
Trying to return a specified number of characters from a gene sequence in R
(3 answers)
Extracting the last n characters from a string in R
(15 answers)
Closed 5 years ago.
Is there a function in R that I can cut a value in vector.
for example i got this vec:
40754831597
64278107602
64212163451
and each vale in the vec i want to cut so from the number pos 3 to 6 for example and get a new vector look like this
7548
2781
2121
and so on
I don't really get why you would like to do this, but here you go:
# assuming it's a character vector
substring(vec,3,6)
# if it's numeric
substring(as.character(vec),3,6)
#output
#[1] "7548" "2781" "2121"
We can use sub
sub(".{2}(.{4}).*", "\\1", v1)
#[1] "7548" "2781" "2121"
data
v1 <- c(40754831597, 64278107602, 64212163451)

Transform scaled numbers to raw numbers in R [duplicate]

This question already has answers here:
backtransform `scale()` for plotting
(9 answers)
Closed 8 years ago.
I'm new in R. I would like to transform a set of numbers I have scaled using scale() to the original raw ones.
Here the code I used to scale the numbers
dataCluster <- dataFinal[, c(1)]
data_z <- as.data.frame(lapply(dataCluster, scale))
clusters <- kmeans (na.roughfix(data_z), 3)
where:
dataFinal is a data frame (3 columns x 100 rows)
clusters is a "data matrix" (3 columns x 3 rows).
I would like to create a clustersRaw with the raw values.
Can anyone help?
Don't know it this is going to solve, since you dind't provide your data. However:
#create a matrix 10x3
mat<-matrix(1:30,ncol=3)
#scale it
x<-scale(mat)
#restore it
t(t(x)*attr(x,"scaled:scale")+attr(x,"scaled:center"))

Resources