简体   繁体   English

迭代R中的变量名

[英]Iterating through variable names in R

I'm trying to shorten my code and make it a bit more efficient. 我正在尝试缩短代码并使其更有效率。

My use case is this: 我的用例是这样的:

It will (roughly) take input from a user in order to describe the type of data basically. 它将(粗略地)从用户那里获取输入,以便基本上描述数据的类型。 Then, it takes count of how many of each type. 然后,它计算每种类型的数量。

As it stands right now, it is very inefficient but it works. 就目前而言,效率非常低但是有效。 Currently, my code is this: 目前,我的代码是这样的:

#############
#1 = 2g4    #
#2 = 5g     #
#3 = dual   #
#############

num_of_Antennas = 4

ant1 = 1
ant2 = 2
ant3 = 2
ant4 = 1


#initialize each count to 0
count2g4 = 0
count5g = 0
countDual = 0

if (num_of_Antennas==4){


  #check type for ant1
  if (ant1==1){
    count2g4 = count2g4+1
  } else if (ant1==2){
    count5g = count5g+1
  } else if (ant1==3){
    countDual = countDual+1;
  } 
  ###############

  #check type for ant2  
  if (ant2==1){
    count2g4 = count2g4 +1
  } else if (ant2==2){
    count5g = count5g +1
  } else if (ant2==3){
    countDual = countDual +1;
  }
  ###############

  #check type for ant3  
  if (ant3==1){
    count2g4 = count2g4 +1
  } else if (ant3==2){
    count5g = count5g +1
  } else if (ant3==3){
    countDual = countDual +1;
  }
  ###############

  #check type for ant4  
  if (ant4==1){
    count2g4 = count2g4 +1
  } else if (ant4==2){
    count5g = count5g +1
  } else if (ant4==3){
    countDual = countDual +1;
  }
  ###############




} else if (num_of_Antennas==3){
  #check type for ant1
  if (ant1==1){
    count2g4 = count2g4+1
  } else if (ant1==2){
    count5g = count5g+1
  } else if (ant1==3){
    countDual = countDual+1;
  } 
  ###############

  #check type for ant2  
  if (ant2==1){
    count2g4 = count2g4 +1
  } else if (ant2==2){
    count5g = count5g +1
  } else if (ant2==3){
    countDual = countDual +1;
  }
  ###############

  #check type for ant3  
  if (ant3==1){
    count2g4 = count2g4 +1
  } else if (ant3==2){
    count5g = count5g +1
  } else if (ant3==3){
    countDual = countDual +1;
  }
  ###############




} else if (num_of_Antennas==2){
  #check type for ant1
  if (ant1==1){
    count2g4 = count2g4+1
  } else if (ant1==2){
    count5g = count5g+1
  } else if (ant1==3){
    countDual = countDual+1;
  } 
  ###############

  #check type for ant2  
  if (ant2==1){
    count2g4 = count2g4 +1
  } else if (ant2==2){
    count5g = count5g +1
  } else if (ant2==3){
    countDual = countDual +1;
  }
  ###############

} else if (num_of_Antennas==1){
  #check type for ant1
  if (ant1==1){
    count2g4 = count2g4+1
  } else if (ant1==2){
    count5g = count5g+1
  } else if (ant1==3){
    countDual = countDual+1;
  } 




} else (print("ERROR, Num of antennas = 0"))

I'm a bit of a noob when it comes to coding, but I'm sure theres a better way of doing this. 在编码方面,我有点像菜鸟,但我确信这是一个更好的方法。

I started playing with the idea of turning it into some sort of parametric function, but I'm not finding a good way of doing that just yet. 我开始尝试将其转换为某种参数函数,但我还没有找到一种好方法。 So how can I go about doing this without compromising flexibility? 那么我怎样才能在不影响灵活性的情况下做到这一点呢? I'm also looking to expand this to a 12 antenna maximum, which will increase the number of code for such a simple task EXPONENTIALLY. 我还希望将其扩展到12个天线最大值,这将增加这样一个简单任务的代码数量EXPONENTIALLY。

Thanks in advance! 提前致谢! Cody 科迪

EDIT: As has been pointed out, using factors will be extremely beneficial, but it still leaves me looking for some sort of raw user interface thats a bit easier to follow than just filling in a vector. 编辑:正如已经指出的那样,使用因素将是非常有益的,但它仍然让我寻找某种原始用户界面,比仅填充向量更容易遵循。 I'd like something similar to this: 我想要类似的东西:

#(not working)
ant1 = 1
ant2 = 2
ant3 = 3
ant4 = 1


data = paste(ant1, ant2, ant3, ant4 sep = ",")
data


  ant <- c(data)

  ant_f <- factor(ant, levels=c(1:3),labels =c("2G4", "5G", "Dual"))
  table(ant_f)

Thank you for providing a snippet of your code. 感谢您提供代码片段。 I hope I understood correctly what you are trying to do. 我希望我能正确理解你要做的事情。 I suggest you keep antennas in a vector. 我建议你把天线放在矢量中。 Length of the vector will tell you how many antennas you are trying to count. 矢量长度将告诉您要计算的天线数量。 Here's an example for 12 antennas. 这是12个天线的示例。

#ant <- c(1,2,2,1)
ant <- c(1,2,2,1,3,2,2,3,1,1,3,2)

ant_f <- factor(ant, levels=c(1:3),labels =c("2g", "5g", "dual"))
table(ant_f)
#> 
#> ant_f
#>   2g   5g dual 
#>    4    5    3 

Basically all you need to do it so define interpretation of possible values (factors should work nicely in this case), which will serve as a dictionary. 基本上你需要做的就是定义可能值的解释(因子在这种情况下应该很好地工作),它将作为字典。 Then just count occurrences with table() 然后用table()计算出现次数

You can refer to individual antennas by vector subsetting. 您可以通过向量子集来引用单个天线。 Because it is a factor, it will carry its label with it. 因为它是一个因素,它将带有它的标签。

ant_f[5]
#> [1] dual
#> Levels: 2g 5g dual

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM