- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先对问题标题不明确表示歉意,这是一个非常具体的问题,我不知道如何用一行来表达!
无论如何,我的问题如下。我有一个数据框,其中包含一个 ID、一个父 ID 和两个值,比如 a 和 b。我想将一行的 id 更新为它的 parent_id,除非它的值不等于它的 parent_id。
例如,假设我有一张 table :
id parent_id a b
1 0 x x
2 1 x x
3 1 x y
4 0 y y
5 4 x x
6 1 x x
7 4 y y
可以用代码生成
x <- data.frame('id' = c(1,2,3,4,5,6,7),
'parent_id' = c(0,1,1,0,4,1,4),
'a' = c('x','x','x','y','x','x','y'),
'b' = c('x','x','y','y','x','x','y'))
这应该变成:
id parent_id a b
1 0 x x
1 1 x x
3 1 x y
4 0 y y
5 4 x x
1 1 x x
4 4 y y
所以 id
2 变成了 1,因为那是它的 parent_id
,而且属性 a
和 b
是两者都等于 x
,与 id
1 相同,但是 id
3 保持不变,尽管它的 parent_id
是 1
,它没有相同的属性。
如有任何帮助,我们将不胜感激。
最佳答案
其他人可能有更优雅的解决方案,但这可以满足您的需求:
# list of a-b pairs for parent_id
parent.id.ab <- with(x, lapply(parent_id, FUN=function(y) c(a[id==y], b[id==y])))
# list of a-b pairs for id
id.ab <- with(x, mapply(function(y,z) c(y,z), a, b, SIMPLIFY=FALSE))
# condition is a vector of TRUE/FALSE, TRUE if the parent_id a-b pair equals the id a-b.
# When the parent_id is 0, its a-b pair is integer(0). Since all(logical(0)) is TRUE,
# we only use all(z == y) when z and y have the same length.
condition <- mapply(function(z,y) if (length(z) == length(y)) all(z == y) else FALSE,
parent.id.ab, id.ab)
x$id <- ifelse(condition, x$parent_id, x$id)
关于r - 更新 id 到它的 parent_id 除非它有不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11126301/
我有一个表,其中 reach 行使用 parent_id 字段链接到自身。 人员表:ID姓名年龄parent_id 当我删除一行时,我希望子行重新链接到正在删除的行的父行。 这可以使用约束来完成吗?或
在一般的一对多(父对子)关系中,(a) 将 parent_id 放入子表与 (b) 使用仅包含 parent_id、child_id 的数据透视表之间是否存在显着的效率差异? 注意:如有必要,假设使用
我使用 simple_form。使用 rails accepts_nested_attributes_for 时,表单验证失败,提示嵌套属性验证失败,因为父对象的 id 不能为空 (validates
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我有表: 类别 +----+-----------+---------------+ | id | parent_id | name | +----+-----------+----
我是 SQL 新手,正在尝试理解外键语法。我知道这是在多个问题中提出的,但我发现的每个问题似乎都没有告诉我我在这里做错了什么。这是我的 SQL 代码: CREATE TABLE Customer (
我有一个具有这种结构的表: CREATE TABLE `models` ( `id` bigint(10) unsigned not null auto_increment, `par
我有这张表oc_review: review_id product_id customer_id author text rat
我有一个名为类别的表。该表包含类别及其子类别... 这是一个简单的表格: ID parent_id 标题 这样我就可以创建具有无限深度的类别...我有点希望cakephp能够理解parent_id(我
我有一个商店数据库,其中包含商店列表、它们的 ID 以及 5 行对应 5 级 parent /祖 parent ID 的信息。 我还有商店信息,例如地址、电话号码、产品等,但所有这些行都位于一个单独的
尝试找出如何按 id 对客户记录进行排序,除非 parent_id(也存在于客户表中)等于父级的 id记录。 所需顺序: 等等.... 使用原始 sql,Postgres 数据库。到目前为止没有运气!
我假设这不是简单的方法,但这是我的家庭作业,所以请耐心等待。 我在 Mysql 'menu' 中有一个表,其结构如下: id - number parent_id - number (0 for r
您好,我在验证时遇到问题,我正在使用 yii2 高级应用程序。 我在创建菜单时有一个 parent_id,我给出菜单名称并提供该菜单是否为父菜单的天气,如果父菜单复选框被选中,如果没有,则将从下拉菜单
我有一个像这样的类别对象: def parent_default(context): ''' default parent id if the name is not root '''
首先对问题标题不明确表示歉意,这是一个非常具体的问题,我不知道如何用一行来表达! 无论如何,我的问题如下。我有一个数据框,其中包含一个 ID、一个父 ID 和两个值,比如 a 和 b。我想将一行的 i
我们有一个非常旧的自定义数据库(oracle、mysql、derby),有限制:没有新表字段、没有 View 、没有函数、没有过程。 我的表MYTABLE: | id | ... | pare
所以我有一个网络程序来上传包含这样的表的 TSV 文件: ------------------------------------------------ SANDI PENDAPATAN
我有一个看起来与此类似的表格: rule_id parent_id 1 0 2 0 3 2 4 2 5
我想知道实现以下结果的最简单方法是什么? table_companies co_id | co_parent_id | co_title ------|--------------|---------
我正在使用 Laravel(当然还有 MySQL)构建一个页面模块,一个页面可以有 1 个父页面 这是我当前的数据,使用 order_by="id" 也是这样返回的。 +----+----------
我是一名优秀的程序员,十分优秀!