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"))
Related
This question already has answers here:
How to get the mean of specific columns in dataframe and store in vector (in R)
(1 answer)
Find mean of multiple columns in R
(4 answers)
Closed 2 months ago.
I have a large dataset. I want to calculate the mean of some of the columns in the data set together. I am not sure how I can use the
colMeans ()
I have only found how to calculate for categories and rows.
Let me take embedded data iris in R as an example.
colMeans(iris[, 1:3], na.rm=TRUE) # select columns #1~3.
This question already has answers here:
Sample random rows in dataframe
(13 answers)
Closed 3 years ago.
Suppose I have a dataset with (90,000 x 17) i.e. (n x p) where n is the number of observations and p is the number of variables and I would like to take a random sample of 20% of rows from my whole dataset how can this be done in R?
After taking a random sample I will be performing cluster analysis accordingly.
I had tried using other questions to answer my question but they were inconclusive because it was not giving me what I needed.
You can do it with sample_frac from dplyr, here is an example with the database iris
library(dplyr)
#data(iris)
sample20 <- iris %>% sample_frac(0.2)
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))
This question already has answers here:
Generate N random integers that sum to M in R
(3 answers)
Closed 7 years ago.
So this seems like a really simple question to me, but I can't seem to figure it out. I'm using R and I'm trying to generate a random sample, where the generated sample all sums to a set number.
Here's an approach to consider. Generate some random numbers:
n = 10
x <- runif(n) # or rnorm, rpois, whatever you want to use
And then scale() them to get the sum you want.
tot = 100 # this is the sum you want
x <- scale(x, center=FALSE, scale=sum(x)/tot)
all.equal(sum(x), tot) #TRUE
This question already has answers here:
Randomly insert NAs into dataframe proportionaly
(6 answers)
Closed 7 years ago.
for testing purposes I need to add missing values to a data frame which has no missing values, how can I add 10% random NAs to my data frame:
dat <- data.frame(v1=rnorm(20),v2=rnorm(20),v3=rnorm(20))
my idea was something like:
a <- sample(1:nrow(dat),3,replace=F)
b <- sample(1:ncol(dat),2,replace=F)
dat[a,b] <- NA
but this is not random enough. thanks.
It seems like you're asking for a way to create true random numbers, rather than how to implement it. If this is the case, you could use the random package available through CRAN which can sample random integers from random.org
install.packages("random")
require("random")
The details of the package: http://cran.r-project.org/web/packages/random/index.html
I suggest you look especially at the vignette, 'random: an R package for true random numbers' for how to obtain random integers.