As.character返回数字而不是字符串R
我指的是:As.character返回数字而不是字符串R,r,data.table,R,Data.table,我指的是: > 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"
> 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"
从结构上看:
> 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>
str(dt[1])
类“data.table”和“data.frame”:1个obs。在12个变量中:
$RMove:系数w/9级“1_b”、“2_a”、“2_c”、..:1
$YMove:系数w/6级“1_a”、“1_b”、“2_b”、..:1
$b移动:系数w/6级“1_b”,“2_c”,“3_b”,“1”
$Red:系数w/23级“-0.5”、“-1.5”、..:18
$黄色:系数w/21级“-0.5”、“-1.5”、..:6
$Blue:系数w/18级“-1”、“-1.5”、“-12”、..:9
$Rm:系数w/4级“1”、“2”、“3”、“4”:1
$Ym:系数w/4级“1”、“2”、“3”、“4”:1
$Bm:系数w/4级“1”、“2”、“3”、“4”:1
$Rchar:系数w/3级“a”、“b”、“c”:2
$Ychar:系数w/2“a”、“b”级:1
$Bchar:系数w/2“b”、“c”级:1
-属性(*,“.internal.selfref”)=
所以我明白了,我已经把这变成了因素。但我不清楚每行末尾的数字代表什么(比如RMove行末尾的1),如果这些数字在列表中,我如何让它显示字符(“1_a”)
如果你想看看我用来达到这一点的所有代码。当我想在表上使用拆分时,因子位于底部:
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))
PieceValue=data.frame(“a”=3,“b”=5,“c”=7)
颜色代码=数据帧(“R”=1,“Y”=2,“B”=3)
#设置启动条件
L1=c(“Ra”、“Rc”、“Bc”)
Z1=2
L2=c(“Rb”、“Bb”、“Ya”)
Z2=2
L3=c(“Yb”)
Z3=0
L4=c()
Z4=0
#样本策略
#SR=“3-c”
#SB=“2-c”
#SY=“2-b”
#为每个玩家设置的策略
ROne=c(“1_b”)
RTwo=c(“2_a”,“2_c”)
rTree=c(“3_a”、“3_b”、“3_c”)
RFour=c(“4_a”、“4_b”、“4_c”)
strestred=列表(ROne、RTwo、Rtree、RFour)
YOne=c(“1_a”,“1_b”)
YTwo=c(“2_b”)
Y树=c(“3_a”)
YFour=c(“4_a”、“4_b”)
strestyellow=list(YOne、YTwo、ytree、YFour)
骨=c(“1_b”)
BTwo=c(“2_c”)
b三个=c(“3个b”,“3个c”)
BFour=c(“4_b”,“4_c”)
strestblue=list(BOne、BTwo、BThree、BFour)
#检查房间的安全
安全=功能(Li,Zi){
if(长度(Li)>Zi){
返回(真)
}否则{
返回(假)
}
}
#计算那个房间的支出
#支付错误
支出=功能(Li,Zi){
温度=c(0,0,0)
Payo=c(0,0,0)
if(安全(Li,Zi)=假){
因为(我在李){
z=strsplit(i,“”[[1]]
点=分段值[[z[2]]]
播放器=颜色代码[[z[1]]]
临时[玩家]=-点
临时[-玩家]=(点数)/2
#错误检查
#打印(点)
#打印(播放器)
#打印(临时)
#印刷品(一)
支付=支付+临时
#打印(付款)
}
}
回报(支付)
}
#计算策略的支出
#添加不存在字符的错误
#确保所有移动都是可能的,否则会出错
#修复了(?)其他错误,仍然需要进行反弹
EvalPay=功能(SR、SY、SB){
颜色=c(“R”、“Y”、“B”)
Strat=c(SR、SY、SB)
房间=列表(L1、L2、L3、L4)
容量=c(4,4,3,99)
僵尸=c(Z1,Z2,Z3,Z4)
收益=c(0,0,0)
(我在1:4中){
位置=房间[[i]]
zeds=僵尸[i]
#打印(“大循环”)
对于(1:3中的j){
#打印(“小循环”)
s=Strat[j]
si=strsplit(s,“”)[[1]]
c=颜色[j]
位置=grep(粘贴(c,si[2],sep=”“),位置,
值=真,反转=真)
如果(i==si[1]){
#如果房间满了,他们就去停车场
当在列表对象上使用as.character
时,if(Capacity[i]问题涉及类似的问题
解决办法很简单
> as.character(unlist(dt[1]))
[1] "1_b" "1_a" "1_b" "0" "0" "0" "1" "1" "1" "b" "a" "b"
我认为您的实际问题是,您对[
和[[
之间的区别感到困惑。请使用[[
再试一次。嗯,我想我明白您的意思了。as.character(dt[[1]])或as.character(dt$RMove)确实返回了我想要的结果。但是,我仍然对最后的因素和数字感到有点困惑。这里的混乱是由dt[1]这一事实造成的
对于data.table的行为与对于data.frame的行为不同。在您的情况下,对于data.table,您将获得第一行,R正试图将其转换为字符。我假设您将整数代码转换为字符。这再次提醒我,我必须了解关于R的更多信息,谢谢!您发布的这段庞大代码,是否有必要你想解释一下你在标题中提到的这个问题吗?