gpt4 book ai didi

r - 如何将 readline() 的输入从字符强制转换为数字

转载 作者:行者123 更新时间:2023-12-01 12:24:51 25 4
gpt4 key购买 nike

我是 R 的新用户,我正在编写一个函数,使用 readline() 提示用户输入数据。我希望稍后在函数中的公式中使用答案。

我收到错误消息,“w/m 错误:二元运算符的非数字参数”

我发现用户输入的是字符类,我想将其转换为数字,以便我可以通过 (wh) 公式运行它。

我的代码看起来像这样......

lands<-function(w, g, r, b, u, l, au, mc, uw, gw, wr, bw, gr, gb, ug, rb, ur, ub){

l<-readline("How many lands are you playing?")

au<-readline("How many of these lands can be tapped for more than or less than 2 colors of mana? note: omit fetch lands that get two different types of land, but include fetch lands that can get any basic.")

mc<-readline("Count the number of mana symbols on your cards an input the number. Be sure to include flash back costs that differ from regular mana costs, hybrid mana should be split (add 1 for odd numbers).")

w<-readline("How many mana symbols are white?")
wh<-(((w/mc)*(l-au))) ##-((uw+gw+wr+bw)/2))
}

最佳答案

需要通过as.numeric进行转换

foo <- as.numeric(readline("Question text? "))

关于r - 如何将 readline() 的输入从字符强制转换为数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23961888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com