i have the following code in R
M <- matrix(NA, nrow = length(27:45), ncol = 2)
colnames(M) <- c("Age", "Whole Life Insurance Benefits")
i<-1
for (i in seq(27, 45, by=1)) {
M[i,1]<- i
M[i,2]<- Axn(actuarialtable = lc.lifetable, x=i)
i<-i+1
}
M
and when i run it i have the error subscript out of bounds
but if i change nrow=length(0:45) it runs without errors but i have a lot of NAs in my matrix. Can you please tell me what is wrong?
Thank you
You have 2 problems in your code :
1. with nrow=length(27:45) you just have 19 rows, but your sequence in if function starts from 27 that not exist
at all in your matrix, you must start it from 1.
2. when you used if function it sums counter by 1 automatically, if you insert i<-i+1 in your loop, the counter
sums by 2, and also you can use 1:19 instead of
seq(1,19,by=1)
Here is correct and simple code :
M <- matrix(NA, nrow = length(27:45), ncol = 2)
colnames(M) <- c("Age", "Whole Life Insurance Benefits")
i<-1
for (i in 1:length(27:45)) {
M[i,1]<- i
M[i,2]<- Axn(actuarialtable = lc.lifetable, x=i)
}
M
Related
This is really hard to explain but basically I have a dataset where people completed a wordsearch task. By using the following code I indexed the letters of the wordsearch by finding their numbers in the descriptions. I put them into a matrix so that I can use them to index from the dataset later:
number <- "#101"
wordsearch <- matrix(rep(0, times = 16 * ncol(data)), nrow = 16, ncol = ncol(data))
for (i in 1:9){
for (j in 1:ncol(data)){
wordsearch[i,j] <- grepl(number, data[1, j], fixed = T)
}
number <- paste("#10", (i+1), sep = "")
}
number <- "#110"
for (i in 10:15) {
for (j in 34:217){
wordsearch[i,j] <- grepl(number, data[1, j], fixed = T)
}
number <- paste("#1", (i+1), sep = "")
}
number <- "#3"
for (j in 1:ncol(data)){
wordsearch[16,j] <- grepl(number, data[1, j], fixed = T)
}
This part works perfectly. Then I want to sum the number of letters people found for each word and create new columns for each word and add to the dataset. First I got the error that 'x must be numeric' so I did data[is.na(data] <- 0
And then I did the following code:
col <- seq(261, by = 1, length.out = 16)
for (i in 1:16){
d2[, col[i]] <- rowSums(d2[, wordsearch[i, ] == 1])
}
I literally just did that with another dataset and it worked fine but now I'm getting the "x' must be an array of at least two dimensions". Can someone help?
I know I can use expand.grid for this, but I am trying to learn actual programming. My goal is to take what I have below and use a recursion to get all 2^n binary sequences of length n.
I can do this for n = 1, but I don't understand how I would use the same function in a recursive way to get the answer for higher dimensions.
Here is for n = 1:
binseq <- function(n){
binmat <- matrix(nrow = 2^n, ncol = n)
r <- 0 #row counter
for (i in 0:1) {
r <- r + 1
binmat[r,] <- i
}
return(binmat)
}
I know I have to use probably a cbind in the return statement. My intuition says the return statement should be something like cbind(binseq(n-1), binseq(n)). But, honestly, I'm completely lost at this point.
The desired output should basically recursively produce this for n = 3:
binmat <- matrix(nrow = 8, ncol = 3)
r <- 0 # current row of binmat
for (i in 0:1) {
for (j in 0:1) {
for (k in 0:1) {
r <- r + 1
binmat[r,] <- c(i, j, k)}
}
}
binmat
It should just be a matrix as binmat is being filled recursively.
I quickly wrote this function to generate all N^K permutations of length K for given N characters. Hope it will be useful.
gen_perm <- function(str=c(""), lst=5, levels = c("0", "1", "2")){
if (nchar(str) == lst){
cat(str, "\n")
return(invisible(NULL))
}
for (i in levels){
gen_perm(str = paste0(str,i), lst=lst, levels=levels)
}
}
# sample call
gen_perm(lst = 3, levels = c("x", "T", "a"))
I will return to your problem when I get more time.
UPDATE
I modified the code above to work for your problem. Note that the matrix being populated lives in the global environment. The function also uses the tmp variable to pass rows to the global environment. This was the easiest way for me to solve the problem. Perhaps, there are other ways.
levels <- c(0,1)
nc <- 3
m <- matrix(numeric(0), ncol = nc)
gen_perm <- function(row=numeric(), lst=nc, levels = levels){
if (length(row) == lst){
assign("tmp", row, .GlobalEnv)
with(.GlobalEnv, {m <- rbind(m, tmp); rownames(m) <- NULL})
return(invisible(NULL))
}
for (i in levels){
gen_perm(row=c(row,i), lst=lst, levels=levels)
}
}
gen_perm(lst=nc, levels=levels)
UPDATE 2
To get the expected output you provided, run
m <- matrix(numeric(0), ncol = 3)
gen_perm(lst = 3, levels = c(0,1))
m
levels specifies a range of values to generate (binary in our case) to generate permutations, m is an empty matrix to fill up, gen_perm generates rows and adds them to the matrix m, lst is a length of the permutation (matches the number of columns in the matrix).
I am running a poisson on a given matrix in R. And for some reasons, I would like to avoid any rowSums and colSums equal to 5 in example. I add repeat in my code to solve this issue but the run never end, even for small matrix.
Any ideas?
Thanks
mat <- matrix(c(seq(1,60,1)), nrow = 6, ncol = 10, byrow = TRUE)
poiss <- function(mat) {
repeat{
pmat <- rpois(length(mat), 1:3)
dim(pmat) <- dim(mat)
if(any(rowSums(pmat)) == 5 | any(colSums(pmat)) == 5) break
}
return(pmat)
}
p = poiss(mat)
I spend multiple hours of thinking about the following problem. I am running a simulation study and I want to define functions outside the simulation study in order to be able to call these functions in the end of my code.
This example illustrates the problem, but is not replicable (below you will find a replicable example of the problem). I make use of the "metafor" package for doing a meta-analysis.
I would like to use the following function that I define outside my final simulation code:
mat <- matrix(NA, nrow = 8, ncol = 3)
funtr.stu <- function(i) {
for (y in 1:8) {
mat[y,i] <- tr[[y]]$k0
}
return(mat)
}
"tr" is a list and consists of the results of 8 times an analysis. I want to retrieve the object "k0" from that list and store it into the matrix "mat".
In the following part of the code (in which I run the simulation), I want to call the function and fill the matrix "mat" with the correct numbers.
for (i in 1:iterations) {
tr.stu <- funtr.stu()
}
The result of this code is a filled matrix, but within each column the same numbers. Thus, R isn´t storing the numbers every iteration, but stores only the last iteration.
How can I modify my code in such a way that R is storing the output as I want?
A very simplified example:
Mat represents just a matrix with numbers and res is an empty matrix that I want to fill.
mat <- matrix(data = c(1,2,3,4,5,6), ncol = 2, nrow = 3)
res <- matrix(NA, ncol = 2, nrow = 3)
I use the function "fun" to fill the empty matrix res.
fun <- function() {
for (i in 1:2) {
res[y,i] <- mat[y,i]
}
return(res)
}
This is what I would like to put in the end of my code (I just want to call the function and with this function I want to fill the matrix "res"). However, if I use the code below R only fills the third row and not the first and second row.
for (y in 1:3) {
test <- fun()
}
Thank you in advance!
This should work in your case. Basically, return one row in each iteration of the for loop. Where as you are returning the entire 'res' matrix.
mat <- matrix(data = c(1,2,3,4,5,6), ncol = 2, nrow = 3)
res <- matrix(NA, ncol = 2, nrow = 3)
fun <- function() {
for (i in 1:2) {
res[y,i] <- mat[y,i]
}
return(res[y,])
}
for (y in 1:3) {
test[y,] <- fun()
}
I spend multiple hours of thinking about the following problem. I am running a simulation study and I want to define functions outside the simulation study in order to be able to call these functions in the end of my code.
A very simplified example:
Mat represents just a matrix with numbers and res is an empty matrix that I want to fill.
mat <- matrix(data = c(1,2,3,4,5,6), ncol = 2, nrow = 3)
res <- matrix(NA, ncol = 2, nrow = 3)
I use the function "fun" to fill the empty matrix res.
fun <- function() {
for (i in 1:2) {
res[y,i] <- mat[y,i]
}
return(res)
}
This is what I would like to put in the end of my code (I just want to call the function and with this function I want to fill the matrix "res"). However, if I use the code below R only fills the third row and not the first and second row.
for (y in 1:3) {
test <- fun()
}
My question is: why isn't R also filling the first and second row and how can I change my code in such a way that R provides me with the desired result?
Thank you in advance!
EDIT:
The following example also illustrates my problem. I make use of the "metafor" package for doing a meta-analysis.
I would like to use the following function that I define outside my final simulation code:
mat <- matrix(NA, nrow = 8, ncol = 3, dimnames = list(c("0_le", "0_ri", ".13_le", ".13_ri", ".33_le", ".33_ri", ".5_le", ".5_ri"), c("1", "2", "3")))
funtr.stu <- function(i) {
for (y in 1:8) {
mat[y,i] <- tr[[y]]$k0
}
return(mat)
}
"tr" is a list and consists of the results of 8 times an analysis. I want to retrieve the object "k0" from that list and store it into the matrix "mat".
In the following part of the code (in which I run the simulation), I want to call the function and fill the matrix "mat" with the correct numbers.
for (i in 1:iterations) {
kip <- funtr.stu()
}
The result of this code is a filled matrix, but within each column the same numbers. Thus, R isn´t storing the numbers every iteration, but stores only the last iteration.
How can I modify my code in such a way that R is storing the output as I want?
Thank you in advance for your help!
It is because you are overwriting all values of the matrix test in each iteration. I added print(test) in the loop. See the code.
mat <- matrix(data = c(1,2,3,4,5,6), ncol = 2, nrow = 3)
res <- matrix(NA, ncol = 2, nrow = 3)
mat
res
fun <- function() {
for (i in 1:2) {
res[y,i] <- mat[y,i]
}
return(res)
}
for (y in 1:3) {
test <- fun()
print(test)
}
This should work in your case. Basically, return one row in each iteration of the for loop. Where as you are returning the entire 'res' matrix.
mat <- matrix(data = c(1,2,3,4,5,6), ncol = 2, nrow = 3)
res <- matrix(NA, ncol = 2, nrow = 3)
fun <- function() {
for (i in 1:2) {
res[y,i] <- mat[y,i]
}
return(res[y,])
}
for (y in 1:3) {
test[y,] <- fun()
}