【问题标题】:How to deal with the object class conversion in a for loop如何处理for循环中的对象类转换
【发布时间】:2017-01-03 23:28:00
【问题描述】:

请注意:否决票对我没有帮助,只会通过降低其知名度来阻碍我解决这个问题。我请求 SO 社区评论如何改进这个问题以解决我的问题。

我有一个数据框,其中包含学生(Amy、Bob)的多个日期 (3),我想对学生和日期的子集执行某些操作。请考虑通过带有对象类转换的for循环过程来回答问题,而不是给出单行代码来解决这个问题。我需要在循环内运行两个函数。 第一个需要一个带有 (A,B) 的数字矩阵,第二个需要一个列表。 求和和除法运算只是为了说明。样本数据:

dput(jj)
structure(list(month = structure(c(1, 2, 3, 1, 2, 3, 1, 2, 3, 
1, 2, 3), class = "Date"), student = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Amy", "Bob"), class = "factor"), 
    A = c(9, 7, 6, 8, 6, 9, 3, 2, 1, 5, 6, 5), B = c(6, 7, 8, 
    5, 6, 7, 5, 4, 6, 3, 1, 5)), .Names = c("month", "student", 
"A", "B"), row.names = c(NA, -12L), class = "data.frame")

#necessary 表示由于对象类要求而假定为强制的步骤。这是代码:

   dong<-data.frame()
IDs<-unique(jj$student)
uniq <- unique(jj$month)
king<-list() #necessary

for (i in IDs ){ 
  for (j in uniq){
  tmp <- jj[jj$student==IDs[i]& jj$month==uniq[j],] 
  tmp$month<-NULL
  tmp$student<-NULL
  tmp1 <- `dimnames<-`(as.matrix(tmp), NULL) #necessary
  #storing column sums as list element

  king[[1]][1]<- sum(tmp1[,1])
  king[[1]][2]<- sum(tmp1[,2])


  kong <- `dimnames<-`(do.call(cbind, lapply(king, as.numeric)), NULL) #necessary
  #dividing column sums
  bong<- kong[,1]/kong[,2]
  dong<-rbind(dong,c(i,j,bong))

}}

但我得到了

Error in `*tmp*`[[1]] : subscript out of bounds

我也很困惑如何将每个学生和月份子集的操作结果保存在单个数据框中。 输出应如下所示:

 #       month student         Bong
    #1 1970-01-02     Amy 1.5454545
    #2 1970-01-03     Amy 1.0000000
    #3 1970-01-04     Amy 1.0000000
    #4 1970-01-02     Bob 1.0000000
    #5 1970-01-03     Bob 1.6000000
    #6 1970-01-04     Bob 0.5454545

谢谢

【问题讨论】:

  • 您的预期输出是什么?从代码中您要尝试做什么并不完全清楚

标签: r list for-loop matrix dataframe


【解决方案1】:

一个直接的问题是tmp1 中没有名为1 的变量,因此您试图对一个不存在的变量求和。你的线路

tmp1 <- `dimnames<-`(as.matrix(tmp), NULL) #necessary

去除矩阵的维度名称,因此它们被重置为 R 变量(V1、V2、...)的默认值。

此外,矩阵要求所有元素的类型相同。由于您有字符列,因此所有列都将转换为字符格式,并且如果不强制返回数字,您的总和将不起作用。

【讨论】:

  • 请立即查看。我已经编辑了代码。并为小费 +1。
【解决方案2】:

这是一个选项:

x <- by(jj, list(jj$month, jj$student), with, sum(A) / sum(B))
y <- as.table(x)
names(dimnames(y)) <- c('month', 'student')

列联表形式的结果

y
#            student
#month              Amy       Bob
#  1970-01-02 1.5454545 1.0000000
#  1970-01-03 1.0000000 1.6000000
#  1970-01-04 1.0000000 0.5454545

或作为数据框

as.data.frame.table(y, responseName='C')
#       month student         C
#1 1970-01-02     Amy 1.5454545
#2 1970-01-03     Amy 1.0000000
#3 1970-01-04     Amy 1.0000000
#4 1970-01-02     Bob 1.0000000
#5 1970-01-03     Bob 1.6000000
#6 1970-01-04     Bob 0.5454545

【讨论】:

  • 您显示的数据框输出正是我需要的。实际上我需要在循环内运行两个函数。第一个需要一个带有(A,B)的数字矩阵,第二个需要一个列表。 bong
  • C 列是第一行中sum(A) / sum(B) 的结果。如果需要,可以用更复杂的表达式替换它,只要它只涉及变量AB
  • 你没有得到我想要的。我需要在循环内运行两个函数。第一个需要一个带有 (A,B) 的数字矩阵,第二个需要一个列表。
  • @PolarBear 是的,我不明白。
  • 请按照我在问题中显示的过程进行操作。它必须使用两个函数,一个需要数字矩阵,另一个需要列表才能得出答案。
猜你喜欢
  • 1970-01-01
  • 2020-06-06
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多