0

I have a dataframe called perceptionAvailInfo which contains the below columns I hope to iterate through->

names(perceptionAvailInfo)
##  [1] "email"          "HomeAddress"    "HomePhone"      "Cellphone"     
##  [5] "Employer"       "PoliticalAffln" "WrittenWorks"   "Photo"         
##  [9] "Video"          "Groups"         "Birthdate"      "sex"           
## [13] "age"            "employ"

My code is as below ->

for (i in names(perceptionAvailInfo)) {
  perceptionAvailInfo[i]
}

I have tried printing just i,perceptionAvailInfo etc inside the for loop but I don't see any output for the forloop. What am I missing?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • 2
    You need to insert `print` or `cat` in your `for` loop. –  Oct 01 '15 at 03:43
  • 1
    Also, read [this answer](http://stackoverflow.com/a/4716380/3710546) for a complete overview. –  Oct 01 '15 at 03:45

1 Answers1

1

Does this work for you?

for (i in names(perceptionAvailInfo)) {
  print(i)
}
Seth
  • 4,745
  • 23
  • 27
  • I tried that but I see the following error => Error in print[i] : object of type 'closure' is not subsettable Calls: ... handle -> withCallingHandlers -> withVisible -> eval -> eval Execution halted – Rithesh Shenthar Oct 01 '15 at 03:58
  • @RitheshShenthar It is because `print` is a function and `[` is used for indexing. You cannot index a function. You need either `print(i)` or `print(perceptionAvailInfo[i])`. –  Oct 01 '15 at 04:04
  • Used print(i) instead of print[i] and it worked! I also saw the post marked by Pascal and it was helpful. – Rithesh Shenthar Oct 01 '15 at 04:05