English 中文(简体)
R 中的矩阵索引索引
原标题:indexing a matrix in R

这里的Novice R 用户。 所以我有一个数据集格式化如下:

    Date  Temp  Month
 1-Jan-90 10.56      1
 2-Jan-90 11.11      1
 3-Jan-90 10.56      1
 4-Jan-90 -1.67      1
 5-Jan-90  0.56      1
 6-Jan-90 10.56      1
 7-Jan-90 12.78      1
 8-Jan-90 -1.11      1
 9-Jan-90  4.44      1
10-Jan-90 10.00      1

在语法中 :

datacl <- structure(list(Date = structure(1:10, .Label = c("1990/01/01", 
  "1990/01/02", "1990/01/03", "1990/01/04", "1990/01/05", "1990/01/06", 
  "1990/01/07", "1990/01/08", "1990/01/09", "1990/01/10"), class = "factor"), 
      Temp = c(10.56, 11.11, 10.56, -1.67, 0.56, 10.56, 12.78, 
      -1.11, 4.44, 10), Month = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
      1L, 1L)), .Names = c("Date", "Temp", "Month"), class = "data.frame", row.names = c(NA, 
  -10L))

我想对特定月份的数据进行子集,然后对临时时间应用一个更改系数,然后保存结果。所以我有类似的东西

idx <- subset(datacl, Month == 1)  # Index
results[idx[,2],1] = idx[,2]+change  # change applied to only index values

但我总是会犯错

Error in results[idx[, 2], 1] = idx[, 2] + change: 
  only 0 s may be mixed with negative subscripts

任何帮助都将不胜感激。

最佳回答

首先,给更改系数一个值:

change <- 1

现在,这就是如何创建索引:

# one approach to subsetting is to create a logical vector: 
jan.idx <- datacl$Month == 1

# alternatively the which function returns numeric indices:
jan.idx2 <- which(datacl$Month == 1)

如果你想仅仅从一月份开始的数据子集

jandata <- datacl[jan.idx,]
transformed.jandata <- transform(jandata, Temp = Temp + change) 

为了保持整个数据框架,但只将变化系数添加到 " 临时 " 中:

datacl$Temp[jan.idx] <- datacl$Temp[jan.idx] + change
问题回答

首先,请注意 subset 不产生索引, 它生成了包含所有行的原始数据框架的子集, 包含 Month = 1

然后,当你在做 idx[,2] ,2]时,你选择了 Temp 列。

results[idx[,2],1] = idx[,2] + change

但随后您又把这些作为 index 输入 results , 也就是说, 您又把它们用作行号。 行号不能是 10.56 -1.11 , 由此产生错误。 另外, 您重新选择了 results 的第一列, 即 date , 并试图增加温度 。

有几种方法你可以做到这一点。

您可以以 Month = 1 FALSE 为一行创建逻辑索引 TRUE , 或

idx <- datac1$Month == 1

然后您可以使用该索引选择您想要修改的 datac1 中的行(这是您最初想要做的,我想):

datac1$Temp[idx] <- datac1$Temp[idx] + change  # 或  results  instead of  datac1 ?

请注意, datac1$Temp[idx] 选择 Temp 一列 datac1 一行 idx

您也可以这样做

datac1[idx, Temp ]

datac1[idx,2]  # as Temp is the second column.

如果您只希望 results 作为 Month = 1 的子集, 请尝试 :

results <- subset(datac1, Month == 1)
results$Temp <- results$Temp + change

这是因为 结果 只包含您感兴趣的行, 所以不需要进行子设置 。

我个人会使用 ifelse () () >, 并用 under () 来调制一个漂亮的衬里 datacl & lt;- in( datacl, Temp & lt;- ifelse (Month = 1, Temp+ change, Temp)) 。 我说了一种衬里, 但是您也需要在其他地方定义 change





相关问题
adding an index to sql server

I have a query that gets run often. its a dynmaic sql query because the sort by changes. SELECT userID, ROW_NUMBER(OVER created) as rownumber from users where divisionID = @divisionID and ...

Linq to SQL nvarchar problem

I have discovered a huge performance problem in Linq to SQL. When selecting from a table using strings, the parameters passed to sql server are always nvarchar, even when the sql table is a varchar. ...

TableView oval button for Index/counts

Can someone help me create an index/count button for a UITableView, like this one? iTunes http://img.skitch.com/20091107-nwyci84114dxg76wshqwgtauwn.preview.jpg Is there an Apple example, or other ...

Move or copy an entity to another kind

Is there a way to move an entity to another kind in appengine. Say you have a kind defines, and you want to keep a record of deleted entities of that kind. But you want to separate the storage of ...