Below piece of code is generating what I need but I am not able to store it so that I can use it further.
In the case below, I want to store each player's hand in a list of matrices p such that p[i]<-deck2[smpl,].
The second thing I want is to save and use the final matrix of deck2 (i.e say with 10 players, it will be a 29 row matrix). I can see NROW(deck2) as 29 but the assignment of d<-deck2 is not happening. What am I missing here?
deck2=matrix(c(rep( c(2:10,"J","Q","K","A"),4),rep(c("C","D","H","S"),rep(13,4))), ncol=2,dimnames=list(NULL,c("rank","suit")))
player_hands=function(players)
{ if(players >= 2 && players <= 10) {
for(i in 1:players)
{
smpl <- sample(1:NROW(deck2),2,replace=F)
r <- deck2[smpl,]
p <- deck2[smpl,]
deck2 <- deck2[-smpl,]
print(r)
if(i==players)
{ smpl <- sample(1:NROW(deck2),3,replace=F)
r <- deck2[smpl,]
p <- deck2[smpl,]
deck2 <- deck2[-smpl,]
print("Dealer Hand")
print(r)
}
else i=i+1
} }
else print("Invalid No. of Players")
}
I believe this should do what you want. It will return a list containing two items.
The first of these two items is the list of hands p, of which the last one will be the dealer's hand.
The second of the two items it returns will be the new deck2.
player_hands=function(players)
{ if(players >= 2 && players <= 10) {
p = list()
for(i in 1:players)
{
smpl <- sample(1:NROW(deck2),2,replace=F)
r <- deck2[smpl,]
p[[i]] = r
deck2 <- deck2[-smpl,]
if(i==players)
{ smpl <- sample(1:NROW(deck2),3,replace=F)
r <- deck2[smpl,]
p[[players+1]] <- r
deck2 <- deck2[-smpl,]
}
else i=i+1
}
return(list(p, deck2))
}
else print("Invalid No. of Players")
}
Related
I am working with a time-series raster brick. The brick has 365 layers representing a value for each day of the year.
I want to create a new layer in which each cell holds the number of day of year in which a certain condition is met.
My current approach is the following (APHRO being the raster brick), but returns the error message below:
enter code here
r <- raster(ncol=40, nrow=20)
r[] <- rnorm(n=ncell(r))
APHRO <- brick(x=c(r, r*2, r))
NewLayer <- calc(APHRO, fun=FindOnsetDate(APHRO))
Returning this error:
Error in .local(x, ...) : not a valid subset
And the function being parsed:
FindOnsetDate <- function (s) {
x=0
repeat {
x+1
if(s[[x]] >= 20 | s[[x]] + s[[x+1]] >= 20 & ChkFalseOnset() == FALSE)
{break}
}
return(x);
}
With the function for the 3rd condition being:
ChkFalseOnset <- function (x) {
for (i in 0:13){
if (sum(APHRO[[x+i:x+i+7]]) >= 5)
{return(FALSE); break}
return(TRUE)
}
}
Thank you in advance!!!!
And please let me know if I should provide more information - tried to keep it parsimonious.
The problem is that your function is no good:
FindOnsetDate <- function (s) {
x=0
repeat {
x+1
if(s[[x]] >= 20 | s[[x]] + s[[x+1]] >= 20)
{break}
}
return(x);
}
FindOnsetDate(1:100)
#Error in s[[x]] :
# attempt to select less than one element in get1index <real>
Perhaps something like this:
FindOnsetDate <- function (s) {
j <- s + c(s[-1], 0)
sum(j > 20 | s > 20)
# if all values are positive, just do sum(j > 20)
}
FindOnsetDate(1:20)
#10
This works now:
r <- calc(APHRO, FindOnsetDate)
I would suggest a basic two-step process. With a 365-days example:
set.seed(123)
r <- raster(ncol=40, nrow=20)
r_list <- list()
for(i in 1:365){
r_list[[i]] <- setValues(r,rnorm(n=ncell(r),mean = 10,sd = 5))
}
APHRO <- brick(r_list)
Use a basic logic test for each iteration:
r_list2 <- list()
for(i in 1:365){
if(i != 365){
r_list2[[i]] <- APHRO[[i]] >= 20 | APHRO[[i]] + APHRO[[i+1]] >= 20
}else{
r_list2[[i]] <- APHRO[[i]] >= 20
}
}
Compute sum by year:
NewLayer <- calc(brick(r_list2), fun=sum)
plot(NewLayer)
I have a set of data, with it's own metadata. I get some of the columns to list all the data from the given set of data.
Then I use this loops to store it in a matrix (I tried a data.frame and a list, but didn't work either). The entries are strings.
#############
ii_c <- metadades$item_id[metadades$tipus_item == "comentari"]
g_c <- metadades$grup[metadades$tipus_item == "comentari"]
i_c <- metadades$item[metadades$tipus_item == "comentari"]
in_c <- data_ent[, ii_c]
c_l <- list()
for(i in 1:ncol(in_c)){
c_l[[i]] <- in_c[,i][!is.na(in_c[,i])]
}
j <- 0
l <- 0
c_cl <- matrix(ncol=3)
for(i in 1:ncol(in_c)){
if(mode(c_l[[i]])=="numeric"){
j=j+1
} else {
for(k in 1:length(c_l[i])){
c_cl[i-j+l,] = c(g_c[i],i_c[i],c_l[i][k])
l=l+1
}
}
}
df_cl <- as.data.frame(c_cl)
#############
This way afterwards I would be able to plot it. Nevertheless I've tried to list (instead of making a matrix) all the dataframes and later on I could be able to cbind them (but it gave me errors aswell).
The next step would be to do a tableGrob and a grid.draw, to print it in a report.
Got the solution from my workmate,,
df_comentaris <- data.frame(grup=NA, item=NA, comentari =NA)
for (i in metadades$item_id[metadades$tipus_item=='comentari']) {
comentaris <- dades[!is.na(dades[i]),i]
grup <- metadades$grup[metadades$item_id == i]
item <- metadades$item[metadades$item_id == i]
df_aux <- data.frame(grup=rep(grup,length(comentaris)), item=rep(item,length(comentaris)), comentari=comentaris)
df_comentaris <- rbind(df_comentaris, df_aux)
}
df_comentaris <- df_comentaris[2:nrow(df_comentaris),]
i try to compute similarity between a pre-known node and all the other nodes of the graph in R. and at each step, if the similarity exceeds a certain threshold i put the node in a vector, for storing all the nodes in dataframe ( for each node, i will give their similars).
but,this code give only the last node, and his last similar node.
v <- DC2$node[order(-DC2$'Centrality')]
Nei1 <- neighbors(g1,as.character(v[1]),1)
vec <- numeric()
if(length(Nei1) > 0) {
for (i in 1:length(V(g1))) {
Nei2 <- neighbors(g,as.character(V(g1)[i]),1)
k1 <- as.numeric(degree(g1,as.character(v[1])))
k2 <- as.numeric(degree(g1,as.character(V(g1)[i])))
Simhpi <- (length(intersect(Nei1,Nei2)) / min(k1,k2))
if (Simhpi >= 0.5) {
for (j in 1:length(V(g1))) {
vv <- V(g1)[j]
vec[j] <- c(vec,vv$name)
}
}
}
}
nn<- data.frame(node=as.character(v[1]), Nei=vec)
thanks for your help.
I'm trying to use if function to create a vector X that follows the pattern of the if statement, that is length 5. However, when I print X, I get 5 vectors with length 1. How do I fix this
for (i in 1:5) {
if (i <2){
a<-i
}
else {
a<-(i-1)
}
X<-a
print(X)
}
R overwrites the contents of your variables a and X with each loop. To avoid this, you can make X a list, and put your value in a different position with each loop.
X <- list()
a <- list()
for(i in 1:5) {
if(i<2){
a <- i
} else {
a <- i-1
}
X[i] <- a
}
Since your final plan is to create a vector, you may initialize a vector ("X") first, and then add a value ("a") in each 'for' loop.
X = vector("numeric",0)
for (i in 1:5){
if (i<2){
a <- i
}
else{
a <- (i-1)
}
X = c(X, a)
print(X)
}
X
# [1] 1 1 2 3 4
Im trying to creat a function in r that will let me sum data from the last three rows into a new column (i.e. if im looking at day 4 = days 3+2+1)
This is what i've worked out so far, however it doesnt work.
S3<- function(x){
res <- numeric(nrow(x))
for (i in 1:nrow(x)){
res[i] <- i
if (i > 3) {
res[i] <- x[i-3,10]
} else {
res[i] <- x[i,10]
}
}
x$PP3 <- res
return(x)
}
Use this:
x$PP3 <- x[,10] + c(0,head(x[,10],-1)) + c(0,0,head(x[,10],-2))
If you want a function:
S3 <- function(x, j=10){
within(x, PP3 <- x[,j] + c(0,head(x[,j],-1)) + c(0,0,head(x[,j],-2)))
}