Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need help creating a for loop to fill in a 5X5 table using R. Each row will be one observation without replacement. The number range is 1:75, and respectively I have probabilities for each of these numbers. So how would I go about creating a random number generating code that takes into account the specific probability for each number?
Here is some sample data:
A <- seq_len(75)
B <- rpois(75, 3)
B <- B / sum(B)
So now B is a probability vector for each element in A.
To pull 25 samples, simply use sample(A, size = 25, replace = FALSE, prob = B). Fill the matrix as usual MAT <- matrix(sample(A, size = 25, replace = FALSE, prob = B), nrow = 5).
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a vector with 40 values and a dataframe with 41 columns, I'd like to plot the vector as x and the row of the dataframe (except for 1 column) as y. The problem is I don't want to put the vector inside the dataframe and ggplot doesn't allow a vector as argument. Any advice is welcome. Thanks in advance.
GGplot can accept vectors as input. Here is an example of plotting one vector against the first row of a data.frame (with matching length). Does this solve your problem?
xval <- rnorm(40)
df <- as.data.frame(matrix(nrow = 5, ncol = 41, data = rnorm(5*41)))
ggplot() +
geom_point(aes(x = xval, y = unlist(df[1,1:40])))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I created a 4✕5 matrix and use a double loop to populate it with numbers so that the value of an element in the matrix is its row index raised to the power of its column index (e.g. the value of the element in row 2 and column 3 is 2 raised to the power of 3, i.e. 8).
How can I create a double loop and populate it with numbers?
Just use outer.
outer(1:4, 1:5, "^")
This is just a common way of writing it. I'd appreciate if you upvote/accept answer if it solves your problem.
M = matrix(nrow = 4, ncol = 5)
for (i in seq(nrow(M))){
for (j in seq(ncol(M))){
M[i,j] = i^j
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have two columns x and y. I want to have one column which contains the ranking for both columns. I thought about sum both column and then get it ranked, does any one have a function that rank two columns in r?
Many thanks
If you are just wanting to use the rank function as you suggest:
df1 <- data.frame(x = rnorm(10), y = rnorm(10))
apply(df1, 2, rank) # 2 columns with separate rankings
rank(rowSums(df1)) # sum by rows first, then rank
rank(rowMeans(df1)) # avg by rows first, then rank (same result!)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to standardize a data set with mean= 3 and std dev =1/3 ; Is there any command to do it?
Scale is for mean =0 , std dev =1.
Here is a snippet from the documentation of scale (?scale).
Usage
scale(x, center = TRUE, scale = TRUE)
Arguments
x a numeric matrix(like object).
center either a logical value or a vector of length equal to the number of columns of x.
scale either a logical value or a numeric vector of length equal to the
number of columns of x.
The following will do exactly as you wanted.
scale(x, center = 3, scale = 1/3)
As a check, try
(x-3)/(1/3)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How should I predict missing values NA based on other values in R? Mean value is not enough.
All values are dependable - columns values are tree scope rate, rows are three height in meters.
My excel file is here.
Is there any possible way to do that? I've been trying with predict function with no success.
There are a number of ways to go about this but here is one. I also tried using it on your dataset but it's either too small, has too many linear combinations or something else because it's not converging.
Amelia - http://fastml.com/impute-missing-values-with-amelia/
data(mtcars)
mtcars1<-mtcars[rep(row.names(mtcars),10),] #increasing dataset
#inserting NAs into dataset
insert_nas <- function(x) {
len <- length(x)
n <- sample(1:floor(0.2*len), 1) #randomly choosing # of missing obs
i <- sample(1:len, n) #choosing which to make missing
x[i] <- NA
x
}
mtcars1 <- sapply(mtcars1, insert_nas)
ords = c( 'cyl','hp','vs','am','gear','carb' ) #integers - your dataset has no integers so don't specify this
#idvars = c( 'these', 'will', 'be', 'ignored' )
#noms = c( 'some', 'nominal', 'columns' ) #categorical
a.out = amelia( mtcars1, ords = ords)
a.out$imputations[[1]]
#you can also ensemble your imputations if you'd like. Here we ensemble 3 of the 5 returned imputations
final_data<-as.data.frame(sapply(colnames(a.out$imputations[[1]]),function(i)
rowMeans(cbind(a.out$imputations[[1]][,i],a.out$imputations[[2]][,i],a.out$imputations[[3]][,i]))))