相关疑难解决方法(0)

如何从data.frame中删除行而不会丢失属性

对于初学者:我现在在这个问题上搜索了几个小时 - 所以如果答案应该是微不足道的,请原谅我......

我想要做的是从data.frame中删除一行(编号101).它包含测试数据,不应出现在我的分析中.我的问题是:每当我从data.frame中进行子集化时,属性(特别是注释)都会丢失.

str(x)
# x has comments for each variable
x <- x[1:100,]
str(x)
# now x has lost all comments
Run Code Online (Sandbox Code Playgroud)

有充分证据表明,子集将丢弃所有属性 - 到目前为止,它非常清楚.手册(例如http://stat.ethz.ch/R-manual/R-devel/library/base/html/Extract.data.frame.html)甚至建议了一种保留属性的方法:

## keeping special attributes: use a class with a
## "as.data.frame" and "[" method:


as.data.frame.avector <- as.data.frame.vector

`[.avector` <- function(x,i,...) {
  r <- NextMethod("[")
  mostattributes(r) <- attributes(x)
  r
}

d <- data.frame(i= 0:7, f= gl(2,4),
                u= structure(11:18, unit = "kg", class="avector"))
str(d[2:4, -1]) # 'u' keeps its "unit"
Run Code Online (Sandbox Code Playgroud)

到目前为止,我还不知道究竟发生了什么.但是,只需运行这些行(除了最后三行)不会改变我的子集的行为.使用带有适当向量的命令subset()(100次TRUE + 1 FALSE)给出了相同的结果.简单地将属性存储到变量并在子集之后恢复它也不起作用. …

statistics r

19
推荐指数
2
解决办法
3879
查看次数

在提取/替换操作期间保留对象属性的方法

最近我在我的R代码中遇到了以下问题.在一个函数中,接受一个数据框作为参数,我需要添加(或替换,如果它存在)一个列,其中的数据是根据数据框的原始列的值计算的.我编写了代码,但是测试显示我使用的数据框提取/替换操作导致了对象的特殊(用户定义)属性丢失.

在通过阅读R文档(http://stat.ethz.ch/R-manual/R-patched/library/base/html/Extract.html)意识到这一点并确认该行为后,我决定非常简单地解决问题 -通过在提取/替换操作之前保存属性并在之后恢复它们:

myTransformationFunction <- function (data) {

  # save object's attributes
  attrs <- attributes(data)

  <data frame transformations; involves extract/replace operations on `data`>

  # restore the attributes
  attributes(data) <- attrs

  return (data)
}
Run Code Online (Sandbox Code Playgroud)

这种方法奏效了.然而,偶然地,我遇到了另一条R文档(http://stat.ethz.ch/R-manual/R-patched/library/base/html/Extract.data.frame.html),它提供了恕我直言解决相同问题的有趣(并且,可能是更通用的?)替代方法:

## keeping special attributes: use a class with a
## "as.data.frame" and "[" method:

as.data.frame.avector <- as.data.frame.vector

`[.avector` <- function(x,i,...) {
  r <- NextMethod("[")
  mostattributes(r) <- attributes(x)
  r
}

d …
Run Code Online (Sandbox Code Playgroud)

attributes r extract object user-defined

5
推荐指数
1
解决办法
2056
查看次数

make始终重建Makefile目标

我重新设计Makefile了论文项目的大部分文件,以便正确反映工作流程(为项目子目录中的目标创建依赖项的制定规则).但是,在特定的子目录(prepare)中,即使没有依赖项更改,也make 始终重建所有目标.这种意外行为可能是什么原因?

注意:sf.done是一个真实的文件(类型,我称之为"标记文件"),位于不同的子目录中,并在完成数据收集(import)时创建/更新目标的相关步骤transform.

准备/ Makefile文件:

IMPORT_DIR=../import

prepare: import \
         transform \
         cleanup \
         merge \
         sample

import: $(IMPORT_DIR)/sf.done
transform: transform.done
cleanup: cleanup.done
merge: merge.done
sample: sample.done

transform.done: transform.R import
    @$(RSCRIPT) $(R_OPTS) $<
    @touch $@

cleanup.done: cleanup.R transform
    @$(RSCRIPT) $(R_OPTS) $<
    @touch $@

merge.done: merge.R cleanup
    @$(RSCRIPT) $(R_OPTS) $<
    @touch $@

sample.done: sample.R merge
    @$(RSCRIPT) $(R_OPTS) $<
    @touch $@

.PHONY: import transform cleanup merge …
Run Code Online (Sandbox Code Playgroud)

workflow dependencies makefile r

0
推荐指数
1
解决办法
1197
查看次数