如何知道VARCHAR字段的值是否可以成功转换为整数?
我想大规模地将记录从一个表插入另一个表...
所以我有这个列表,我们称之为listA.我试图在每个列表中获取[3]项目,例如['5.01','5.88','2.10','9.45','17.58','2.76']按排序顺序.因此,最终结果将在圣诞老人的顶部重新开始整个列表.这有任何意义吗?
[['John Doe', u'25.78', u'20.77', '5.01'], ['Jane Doe', u'21.08', u'15.20', '5.88'], ['James Bond', u'20.57', u'18.47', '2.10'], ['Michael Jordan', u'28.50', u'19.05', '9.45'], ['Santa', u'31.13', u'13.55', '17.58'], ['Easter Bunny', u'17.20', u'14.44', '2.76']]
我偶然发现了类似的代码
double x,y = ...;
double n = sqrt(x*x+y*y);
if (n > 0)
{
double d1 = (x*x)/n;
double d2 = (x*y)/n;
}
Run Code Online (Sandbox Code Playgroud)
我想知道关于这样的表达的为小的值的数值稳定性x和y.
对于这两个表达式,lim (x->0, y->0) (...) = 0从数学的角度来看,它看起来很安全(分母O(x²)而分母是O(x)).
不过我的问题是:这段代码有任何可能的数值问题吗?
编辑:如果可能的话,我想避免重写表达式,因为n实际上使用了两次并保持可读性(在上下文中相对清楚会发生什么).
假设我使用mtcars数据集.我想根据重量(wt)设置点的大小.如果我这样做,如下所示,R/ggplot2将给我一个4类(2,3,4,5)的图例.
library(ggplot2)
mtc <- mtcars
p1 <- ggplot(mtc, aes(x = hp, y = mpg))
p1 <- p1 + geom_point(aes(size = wt))
print(p1)
Run Code Online (Sandbox Code Playgroud)
如何更改图例的比例/名称/类别.如果"类别"是因素,我找到了有关如何执行此操作的信息,但我不知道如何使用数值来执行此操作.我需要保持数字,否则它不再适用于点的大小.
我的真实数据集有大约100个wt值(从1-150开始),我想保留5个值.(ggplot 2给我2 - > 50和100)
1)如何更改该图例的比例?例如,在mtc示例中,我只想要2个大小为2和5的点
2)我正在考虑制作以下类别:
mtc$wtCat[which(mtc$wt<=2)]=1
mtc$wtCat[which(mtc$wt>2 & mtc$wt<=3)]=2
mtc$wtCat[which(mtc$wt>3)]=3
p1 <- ggplot(mtc, aes(x = hp, y = mpg))
p2 <- p1 + geom_point(aes(size = wtCat), stat="identity")
print(p2)
Run Code Online (Sandbox Code Playgroud)
然后只需将图例中的1,2,3重命名为<= 2,2-3和> 3,但我也不知道如何做到这一点.
非常感谢.
我们正在使用 Algolia 作为我们的搜索引擎。在一个索引中,我们有一个属性:contract.monthDuration,它包含整数。
我成功地使用了以下数字过滤器:
"contract.monthDuration:0 to 3"
Run Code Online (Sandbox Code Playgroud)
但我无法使用类似的东西
"contract.monthDuration:0 to 3" OR "contract.monthDuration:4 to 6"
Run Code Online (Sandbox Code Playgroud)
我试过了 :
"contract.monthDuration:0 to 3 OR contract.monthDuration:4 to 6"
"contract.monthDuration:0 to 3,4 to 6"
"contract.monthDuration:0 to 3, contract.monthDuration:4 to 6"
"(contract.monthDuration:0 to 3, contract.monthDuration:4 to 6)"
Run Code Online (Sandbox Code Playgroud) 我需要编写应输出列表L和K的点积的函数dot(L,K).如果这两个输入列表的长度不相等,则dot应输出0.如果这两个列表都是空的,则点也应输出0.您应该假设输入列表仅包含数值.
这是我到目前为止:
def dot( L, K ):
if len[L]!=len[K]:
return 0
elif L == '' or L == []:
return 0
else:
return sum(L[0]*K[0], L[1]*K[1], ect.)
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我,因为我无法弄清楚在最后一行做什么!
对于我的样本数据集,我使用以下代码将数据从系数转换为数值:
sample = as.data.frame(lapply(sample, function(x) as.numeric(as.character(x))))
Run Code Online (Sandbox Code Playgroud)
然后使用此代码将所有NA值替换为0:
sample[is.na(sample)] = 0
Run Code Online (Sandbox Code Playgroud)
但是,当我从系数转换为数值时,列名称会更改并rownames消失。为什么会发生这种情况?在将所有列转换为数值时如何防止这种情况发生?
dput(sample)
structure(list(`2015-10-08 00:05:00` = structure(c(NA, NA, NA,
NA, 2L, NA), .Names = c("72", "79", "82", "83", "116", "120"), .Label = c(" 1",
" 2", " 3", " 5", "2015-10-08 00:05:00"), class = "factor"),
`2015-10-08 00:12:00` = structure(c(NA, 1L, NA, NA, NA, NA
), .Names = c("72", "79", "82", "83", "116", "120"), .Label = c(" 1",
" 2", " 3", "2015-10-08 00:12:00"), class = "factor"), `2015-10-08 00:34:00` = …Run Code Online (Sandbox Code Playgroud) 我有这个字符向量,但我需要将其转换为数字。
>iono_test_y
[1] "g" "b" "b" "g" "g" "g" "b" "g" "b" "b" "g" "b" "g" "b" "b" "b" "g" "g" "b" "b" "b" "g" "g"
[24] "b" "b" "g" "g" "g" "b" "g" "g" "g" "b" "b" "b" "b" "b" "g" "g" "g" "g" "g" "b" "g" "b" "b"
[47] "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g" "g"
[70] "g" "g"
Run Code Online (Sandbox Code Playgroud)
我已经试过了
iono_test_y <- …Run Code Online (Sandbox Code Playgroud) 我有一个正则表达式问题,它似乎不像我想的那么普遍:我想提取所有具有px单位的数值,应用一些计算,然后将新值重新注入我的字符串中。我不想包含px字符串(请参见下面的示例),但是我可以使用保留它们的另一种方法,或者更改单位类型。
例如,将值乘以2.5:
从 "2px aperture 12px science 2.1px yummy cake"
我想要 "5 aperture 30 science 5.25 yummy cake"
我做了一个粗略的脚本,但是我没有得到想要的输出:
import re
my_string = "2px aperture 12px science 2.1px yummy cake"
nb_list= re.findall(r"([0-9.]+)px", my_string)
splitted_string = re.findall('.*?px', my_string)
print(f"splitted_string = {splitted_string}")
print(f"nb_list = {nb_list}")
new_list = []
for i in range(0, len(nb_list)):
new_n = str(float(nb_list[i])*2.5)
new_string = re.sub(r"[0-9.]+px", new_n, splitted_string[i])
new_list.append(new_string)
new_list = ''.join(new_list)
print(f"new_list = {new_list}")
Run Code Online (Sandbox Code Playgroud)
结果:
new_list = 5.0 aperture 30.0 science 5.25
Run Code Online (Sandbox Code Playgroud)
我知道为什么会得到这个结果,但是我不知道要进行什么更改才能获得所需的输出。
有没有办法可以提取 R 中数值的第一个非零数字?
示例输入:
123.01, 0.56, 0.078, 0.0092
Run Code Online (Sandbox Code Playgroud)
输出:
1, 5, 7, 9
Run Code Online (Sandbox Code Playgroud)