English 中文(简体)
写入一个代码来计算分数并将其添加到数据中。 框架
原标题:Write a code to calculate scores from and add them to a data.frame
  • 时间:2012-05-22 11:30:04
  •  标签:
  • r
  • for-loop

我有6列的数据框架。第一个是实验对象的数据框架,第二个是实验区块的数据框架,而第3、4和5列是我要计算一个二进制分(0或1)的数值,我要在第六列中加上这个数值(这就是为什么现在它满是0)。

head(kfdblock3to9)
    subject time        gr       ugr      sdugr IL
40002.3   40002    3 0.4475618 0.3706000 0.02994533  0
40002.4   40002    4 0.4361786 0.3901111 0.01846110  0
40002.5   40002    5 0.4279880 0.4550000 0.02811839  0
40002.6   40002    6 0.4313647 0.4134444 0.04352974  0
40002.7   40002    7 0.4420889 0.4394286 0.02883143  0
40002.8   40002    8 0.4325227 0.3960000 0.06559222  0

I m trying to do this with a for loop, but I m a beginner in R and I m having difficulties with this. The scoring formula I m trying to implement is one where: If the value in column 3 ($gr) is less that the difference between the value in column 4 ($ugr) and .35 times the value in column 5 ($sdugr), then the subject receives a 1, otherwise a 0.

到目前为止,我试过的是:

for (i in kfdblock3to9$subject) {
     if (kfdblock3to9$gr<(kfdblock3to9$ugr-(.35*kfdblock3to9$sdugr))) 
                 kfdblock3to9$IL=1
         else kfdblock3to9$IL=0
    }

This gives me 50 warnings, all saying: "the condition has length > 1 and only the first element will be used"

我想我当时做错什么指数了,但我一直无法弄清楚,任何帮助都是感激不尽的。

最佳回答

查看 和 ifelse 中的 :

kfdblock3to9 <- 
within(kfdblock3to9,
  IL <- ifelse( gr < ugr - 0.35 * dugr, 1, 0)
)

in () <() 内部并非真正必要,但它使您的代码更加易读易懂,更容易理解。

为什么出错?因为你的状况是矢量化的:尝试

kfdblock3to9$gr<(kfdblock3to9$ugr-(.35*kfdblock3to9$sdugr))

您将会看到它返回一个逻辑矢量。 现在一个 if () 条款只能一次处理一个布尔值。 如果您有矢量结果, 您需要一个矢量解答, 也就是 ifelse ()

问题回答

为了解决你的问题,我建议这样:

kfdblock3to9[, "IL"] <- ifelse(kfdblock3to9$gr < (kfdblock3to9$ugr-(0.35*kfdblock3to9$sdugr)), 1, 0);

(媒介化方法比环更快。 )

您的循环是错误的, 因为您不尊重您的索引 < code> i 。 您必须使用 < code> > i 访问循环中的行 :

for (i in seq(along=kfdblock3to9)) {
    cat("row:", i, kfdblock3to9[i, "subject"], "
");
}

您想要的是一个逻辑测试。 您可以因此避免使用 loop , 甚至 ifelse , 并且简单地做 :

kfdblock3to9$IL <- with(kfdblock3to9, gr < (ugr-0.35*sdugr))

IL 列将包括 FALSE 的 TRUE, 而不是 1 或 0。 如果您更喜欢有整数, 您可以做到 :

kfdblock3to9$IL <- as.integer(with(kfdblock3to9, gr < (ugr-0.35*sdugr)))

在此情况下,您不应使用环。每当您在未来使用环时,您需要使用索引 :

for (i in 1:length(kfdblock3to9$subject)) {
     if (kfdblock3to9[i,"gr"] < (kfdblock3to9[i, "ugr"] - .35 * kfdblock3to9[i, "sdugr"])) 
                 kfdblock3to9[i,"IL"]=1
     else  kfdblock3to9[i,"IL"]=0
}


kfdblock3to9
     subject time        gr       ugr      sdugr IL
40002.3   40002    3 0.4475618 0.3706000 0.02994533  0
40002.4   40002    4 0.4361786 0.3901111 0.01846110  0
40002.5   40002    5 0.4279880 0.4550000 0.02811839  1
40002.6   40002    6 0.4313647 0.4134444 0.04352974  0
40002.7   40002    7 0.4420889 0.4394286 0.02883143  0
40002.8   40002    8 0.4325227 0.3960000 0.06559222  0




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...