我有 50 多列,并查看了各种解决方案,包括这个.
但是,这并不能真正回答我的问题。我有列名,例如:
total_2012Q1, total_2012Q2, total_2012Q3, total_2012Q4,..., up to total_2014Q4, 和其他字符变量。我想按年份添加行,所以最后,我会有三年的列:total_2012, total_2013, total_2014.
我不想rowsum并选择类似 .. 的东西sample[,2:5]。有没有一种方法可以在不手动查看列号的情况下对它们求和?此外,split.default是一个选项,但如果还有字符变量,你如何只处理你想要总结的 int 变量?
简单的可重现示例(预):
id total_2012Q1 total_2012Q2 total_2013Q1 total_2013Q2 char1 char2
1 1231 5455 1534 2436 N Y
2 3948 1239 223 994 Y N
Run Code Online (Sandbox Code Playgroud)
可重现的示例(帖子):
id total_2012 total_2013 char1 char2
1 6686 3970 N Y
2 5187 1217 Y N
Run Code Online (Sandbox Code Playgroud)
感谢您的任何建议。
当满足 2 个条件时,我需要过滤一些行,但不排除其他行。
桌子:
idRow idMaster idList
1 10 45
2 10 46
3 10 47
4 11 10
5 11 98
6 14 56
7 16 28
8 20 55
Run Code Online (Sandbox Code Playgroud)
例子:
什么时候:
预期结果:
idRow idMaster idList
1 10 45
5 11 98
6 14 56
7 16 28
8 20 55
Run Code Online (Sandbox Code Playgroud)
运行 SQL Server 2014
我尝试了 CASE IF 的组合,但所有情况只过滤 idMaster=10,11 和 idList=45,98,不包括其他行
我正在尝试编写一个等级转换器,将 88% 等数字等级更改为字母等级(即 B+),但我觉得有一种比使用无休止的 elif 语句更简单的方法。理想情况下,我希望有一个条件根据范围确定字母,另一个条件确定是否根据单独的范围分配 + 或 - 。这是当前的代码:
def convert_score_to_grade_w_plus_and_minus(score):
if score in range(98, 101):
return 'A+'
elif score in range(93, 98):
return 'A'
elif score in range(90, 93):
return 'A-'
elif score in range(88, 90):
return 'B+'
elif score in range(83, 88):
return 'B'
elif score in range(80, 83):
return 'B-'
elif score in range(78, 80):
return 'C+'
elif score in range(73, 78):
return 'C'
elif score in range(70, 73):
return 'C-'
elif score in range(68, 70): …Run Code Online (Sandbox Code Playgroud) 我有两个表: rais.rais_vinculo 和 code.division
在rais_vinculo 中,我有字段“ CODE ”,其值从 1000 到 99999。
在部门中,我有字段“代码”,值从 1 到 99,字段“描述”。
我想left join()通过匹配第一个数字来将字段“描述”与“代码”放在一起。示例:如果“9000”或“9999”与“9”匹配且“99999”或“99211”与“99”匹配
我拥有的:
代码 | 描述
1 | 农业
... | ...
99 | 其他
和
代码
1000
....
99999
我想要的是:
代码 | 描述代码
1000 | 农业
... | ...
9000 | 行业
9999 | 行业
... | ...
10000 | 商业
... | ...
999999 | 其他
我已经拥有的:
UPDATE rais.rais_vinculo
SET description_code = division.description
FROM code.division …Run Code Online (Sandbox Code Playgroud) 我的代码如下所示:
/*
* A B
* 0 0 -> 1
* 0 1 -> 0
* 1 0 -> 0
* 1 1 -> 0
*/
#define A condition_1
#define B condition_2
if (A) {
// do nothing
} else {
if (B) {
// do nothing
} else {
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
上面我已经报告了1istrue和0is两个条件的真值表false,有没有办法将真值表表达为单个 if 条件?
如何在条件中将变量与其数据类型进行比较?在我的程序(咖啡因吸收计算器)中使用它时,它只是直接跳过任何类型不匹配的输入到最后而不显示错误语句。
一直在移动积木,但似乎没什么区别
#include <typeinfo>
double cafContent;
...
cout << "Enter milligrams of caffeine: " << endl;
cin >> cafContent;
if (typeid(cafContent) != typeid(double)) {
cout << "Please enter a NUMBER for caffeine content." << endl;
return 0;
}
....
Run Code Online (Sandbox Code Playgroud) 我试图接受用户对问题的输入是或否,并根据答案读回我的变量值。我永远无法让附加到变量的命令起作用,或者我的 if 语句接受是或否。它只是继续“不是一个有效的答案”。请让我知道如何真正让它们在 bash 脚本中工作。我一直在寻找不同的东西来尝试,但似乎没有任何效果。这是我现在所拥有的:
yesdebug='echo "Will run in debug mode"'
nodebug='echo "Will not run in debug mode"'
echo "Would you like to run script in debug mode? (yes or no)"
read yesorno
if [$yesorno == 'yes']; then
$yesdebug
elif [$yesorno == 'no']; then
$nodebug
else
echo "Not a valid answer."
exit 1
fi
Run Code Online (Sandbox Code Playgroud) 我有两个条件 C1 和 C2。我希望我的程序在 C1 和 C2 为真、C1 为真但 C2 不为真、C2 为真但 C1 不为真、C1 和 C2 都不为真的情况下执行不同的操作。
现在我有这个代码:
if C1 and C2:
...
elif C1:
...
elif C2:
...
else:
...
Run Code Online (Sandbox Code Playgroud)
有没有更有效的方法在 Python 中编写代码?
我认为由于某种原因,当我使用逻辑 AND 而不是嵌套的 if 语句时,我的程序不起作用。我制作了一个俄罗斯方块克隆,它具有保持功能,当玩家按下 C 键时可以保持/存储一个块。我有一个布尔值,可以防止玩家无法控制地交换块。
出于某种原因,这有效:
if (bKey[5]) //When C key is pressed
{
if (!pieceHold) //boolean to prevent player from holding the C key and swapping blocks constantly/uncontrollably
{
if (!bPieceHeld) //check to see if player is not holding a block
{
r++;
nHoldPiece = nCurrentPiece; //set empty hold piece into the current piece
nHoldRotation = nCurrentRotation;
nCurrentPiece = nNextPiece; //set the current piece into the next piece
nNextPiece = rng[r - 1];
nCurrentX = nFieldWidth / …Run Code Online (Sandbox Code Playgroud) 在我编程的基于文本的冒险中,我一直在编写 IF 语句以允许多行条件,如下所示:
if certain_input == (
"examine bed" or "look at bed" or "look bed" or "look straw" or "inspect straw" or
"inspect bed" or "search bed" or "examine straw" or "look at straw"
):
print("this input has been fulfilled")
Run Code Online (Sandbox Code Playgroud)
以这种格式工作的唯一条件是“检查床”和“检查床”。每个其他列出的选项在使用时都会返回错误。有什么我想念的吗?
if-statement ×5
c++ ×3
python ×3
sql ×2
algorithm ×1
bash ×1
c++17 ×1
grouping ×1
left-join ×1
linux ×1
postgresql ×1
r ×1
range ×1
rowsum ×1
sql-server ×1
truthtable ×1
types ×1
where-clause ×1