如何处理for循环中的对象类转换 [英] How to deal with the object class conversion in a for loop

查看:275
本文介绍了如何处理for循环中的对象类转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:投票不能帮助我,但只能阻止我通过降低其知名度来解决这个问题。我要求SO社区评论如何改进这个问题,以解决我的问题。



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

  dput(jj)
结构(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()
ID< - 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列总和作为列表元素

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


kong < - dimnames< -`(do.call(cbind,lapply ,as.numeric)),NULL)#necessary
#dividing列总和
bong< - kong [,1] / kong [,2]
dong< -rbind(dong,c我,j,bong))

}}

 `* tmp *`[[1]]中的错误:下标出边界

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

 #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

谢谢

解决方案

一个直接的问题是在$ $ c $中没有名为 1 的变量c> tmp1 ,所以你试图求和一个不存在的变量。你的行

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

剥离矩阵的维度名称,因此它们将重置为R个变量的默认值(V1,V2, ...)



此外,矩阵要求所有元素都是相同的类型。由于您有字符列,所有列都将转换为字符格式,并且您的总和将无法正常工作,而不会强制返回数字。


Kindly note: Down votes do not help me but only hinder me to solve this question by reducing its visibility. And I request the SO community to comment how to improve this question in order to solve my problem.

I have a dataframe that have multiple dates (3) for students (Amy,Bob) and I want to perform certain operations on the subsets of students and dates. Please consider answering the question through the for loop process with the object class conversion rather than giving single line code to solve this problem.I am required to run two functions inside the loop. First one requires a numerical matrix with(A,B) and second one requires a list. The Sum and division operations are there just for illustration. Sample data:

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 denotes the step assumed to be compulsary due to object class requirements. This is the code:

   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))

}}

but I am getting

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

I am also confused how to save my operation result bong for each student and month subset in a single dataframe. The output should look like:

 #       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

Thank you

解决方案

An immediate problem is that there's no variable named 1 in tmp1, so you're trying to sum a nonexistent variable. Your line

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

strips the dimension names of the matrix, so they get reset to the default values for R variables (V1, V2, ...).

Additionally, matrices require all elements to be the same type. Since you have character columns, all columns will be converted to character format, and your sums won't work without coercing back to numeric.

这篇关于如何处理for循环中的对象类转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆