This is what I am referring to:
> dt[1]
RMove YMove BMove Red Yellow Blue Rm Ym Bm Rchar Ychar Bchar
1: 1_b 1_a 1_b 0 0 0 1 1 1 b a b
> as.character(dt[1])
[1] "1" "1" "1" "18" "6" "9" "1" "1" "1" "2" "1" "1"
Looking at the structure:
> str(dt[1])
Classes ‘data.table’ and 'data.frame': 1 obs. of 12 variables:
$ RMove : Factor w/ 9 levels "1_b","2_a","2_c",..: 1
$ YMove : Factor w/ 6 levels "1_a","1_b","2_b",..: 1
$ BMove : Factor w/ 6 levels "1_b","2_c","3_b",..: 1
$ Red : Factor w/ 23 levels "-0.5","-1.5",..: 18
$ Yellow: Factor w/ 21 levels "-0.5","-1.5",..: 6
$ Blue : Factor w/ 18 levels "-1","-1.5","-12",..: 9
$ Rm : Factor w/ 4 levels "1","2","3","4": 1
$ Ym : Factor w/ 4 levels "1","2","3","4": 1
$ Bm : Factor w/ 4 levels "1","2","3","4": 1
$ Rchar : Factor w/ 3 levels "a","b","c": 2
$ Ychar : Factor w/ 2 levels "a","b": 1
$ Bchar : Factor w/ 2 levels "b","c": 1
- attr(*, ".internal.selfref")=<externalptr>
So I get that I've turned this into factors. But I'm unclear on what the numbers at the end of each of these lines represent (Like the 1 at the end of the RMove line) and given that these are in a list how do I get it to display the characters ("1_a")?
And if you would like to see all the code I used to get to this point. The factors come in at the bottom when I want to use split on the table:
PieceValue = data.frame("a" = 3, "b" = 5, "c" = 7)
ColorCode = data.frame("R" = 1, "Y" = 2, "B" = 3)
#Setting the starting Conditions
L1 = c( "Ra","Rc","Bc")
Z1 = 2
L2 = c("Rb","Bb","Ya")
Z2 = 2
L3 = c("Yb")
Z3 = 0
L4 = c()
Z4 = 0
#Sample Strategies
# SR = "3-c"
# SB = "2-c"
# SY = "2-b"
#Strategy set for each player
ROne = c("1_b")
RTwo = c("2_a","2_c")
RThree = c("3_a","3_b","3_c")
RFour = c("4_a", "4_b", "4_c")
StrSetRed = list(ROne,RTwo,RThree, RFour)
YOne = c("1_a","1_b")
YTwo = c("2_b")
YThree = c("3_a")
YFour = c("4_a","4_b")
StrSetYellow = list(YOne,YTwo,YThree, YFour)
BOne = c("1_b")
BTwo = c("2_c")
BThree = c("3_b","3_c")
BFour = c("4_b","4_c")
StrSetBlue = list(BOne,BTwo, BThree, BFour)
#Checks the safety of the room
Safety = function(Li, Zi){
if(length(Li)>Zi){
return(TRUE)
} else{
return(FALSE)
}
}
#Calcuates the payout of that room
#Bug with Payout
Payout = function(Li,Zi){
temp = c(0,0,0)
Payo = c(0,0,0)
if (Safety(Li,Zi) == FALSE){
for(i in Li){
z = strsplit(i,"")[[1]]
point = PieceValue[[z[2]]]
player = ColorCode[[z[1]]]
temp[player] = -point
temp[-player] = (point)/2
#Bug check
# print(point)
# print(player)
# print(temp)
# print (i)
Payo = Payo + temp
# print(Payo)
}
}
return(Payo)
}
#Calculates the payout of the Strategies
#Bug with adding non existing characters
#Make sure all moves are possible otherwise bug
#Fixed(?) other bugs, still have to make the bounce happen
EvalPay = function(SR,SY,SB){
colors = c("R", "Y", "B")
Strat = c(SR,SY, SB)
Room = list(L1,L2,L3,L4)
Capacity = c(4,4,3,99)
Zombies = c(Z1,Z2,Z3,Z4)
Payoffs = c(0,0,0)
for(i in 1:4){
location = Room[[i]]
zeds = Zombies[i]
#print("BigLoop")
for(j in 1:3){
#print("Small Loop")
s = Strat[j]
si = strsplit(s,"_")[[1]]
c = colors[j]
location = grep(paste(c, si[2], sep = ""), location,
value = TRUE, invert = TRUE)
if(i == si[1]){
#If the room is full they bound to the parking lot
if(Capacity[i]<=length(location) || i == 4){
Room[[4]] = c(Room[[4]], paste(c,si[2], sep =""))
# print("Full")
# print(Room[[4]])
} else{
location = c(location, paste(c,si[2], sep = ""))
}
}
}
#To fix bug with the function making room 4 empty
if(i != 4){
Room[[i]] = location
Payoffs = Payoffs + (Payout(location,zeds))
} else {
# print(Payoffs)
# print(Payout(Room[[4]], zeds))
Payoffs = Payoffs + (Payout(Room[[4]], zeds))
}
}
#Bug Check
#print(Room)
#print("EndLoop")
return(Payoffs)
}
#Makes A table of possible strategies given Room Number
#Check bugs
MakeStratTable = function(StrRed,StrYell,StrBlue){
StratTable = matrix(c(NA,NA,NA,NA,NA,NA), ncol = 6)
for(a in StrRed){
for(b in StrYell){
for(c in StrBlue){
pay = EvalPay(a,b,c)
temp = matrix(c(a,b,c,pay), ncol = 6)
StratTable = matrix(rbind(StratTable,temp), ncol = 6)
}
}
}
StratTable = StratTable[-1,]
# colnames(StratTable) = c("Rmove","YMove","BMove","Red","Yellow",
# "Blue")
return(StratTable)
}
#Making the Grand Table
FullTable = function(){
times = 1
for(t in StrSetRed){
for(u in StrSetYellow){
for(v in StrSetBlue){
temp = MakeStratTable(t,u,v)
if(times != 1){
FullTable = matrix(rbind(FullTable,temp), ncol = 6)
} else {
FullTable = temp
}
times = times + 1
}
}
}
colnames(FullTable) = c("RMove","YMove","BMove","Red","Yellow",
"Blue")
return(FullTable)
}
library(data.table)
BestResponse = function(frame, score){
temp = frame[with(frame,order(-(as.integer(score))))]
temp = temp[1,]
return(temp)
}
#Part 2
Total = FullTable()
dt = data.table(Total)
#In order to set up sortable factors
dt$Rm = as.integer(unlist(lapply(strsplit(as.character(dt$RMove), "_"), "[",1)))
dt$Ym = as.integer(unlist(lapply(strsplit(as.character(dt$YMove), "_"), "[",1)))
dt$Bm = as.integer(unlist(lapply(strsplit(as.character(dt$BMove), "_"), "[",1)))
dt$Rm = as.factor(dt$Rm)
dt$Ym = as.factor(dt$Ym)
dt$Bm = as.factor(dt$Bm)
dt$Rchar = as.factor(unlist(lapply(strsplit(as.character(dt$RMove), "_"), "[",2)))
dt$Ychar = as.factor(unlist(lapply(strsplit(as.character(dt$YMove), "_"), "[",2)))
dt$Bchar = as.factor(unlist(lapply(strsplit(as.character(dt$BMove), "_"), "[",2)))
dt2 = split(dt,list(dt$Rm,dt$Ym,dt$Bm,dt$Rchar,dt$Ychar))
If your headings are all numbers, for example years, then R will convert them to character strings and prepend “X” to each heading. You can overcome this behaviour and force R to read the headings as they come. Your headings are still converted to character strings but these are easier to coerce to a numeric value.
In R, there's no fundamental distinction between a string and a character. A "string" is just a character variable that contains one or more characters. One thing you should be aware of, however, is the distinction between a scalar character variable, and a vector.
We can convert to numeric by using as. numeric() function.
This question involves a similar question when using as.character
on a list object.
The solution is easy enough.
> as.character(unlist(dt[1]))
[1] "1_b" "1_a" "1_b" "0" "0" "0" "1" "1" "1" "b" "a" "b"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With