0

I have a dataframe with 12 columns. Basically, I am trying to do a for loop that will go through each column and store each column as its own vector. I want to be able to use each vector as an input for another package.

for (i in 1:ncol(df)) {
  #extract each column from left to right one by one
  
  #store each of these columns in their own vector

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
  • `df[, 1]` for example gives you the first column as vector. Why storing it in it's own vector? – Martin Gal Sep 02 '21 at 21:13
  • @MartinGal yes that could work, but i wanted to iterate through whole dataframe to get every column as a vector so i can call whichever column vector i wanted. Any ideas? – Gerald81645 Sep 02 '21 at 21:24
  • 1
    May I ask: What are you trying to do? Since the data.frame is basically a list of vectors that you can access via an index, I don't see any need for storing the columns in separate vectors. The (automated) creation of those vectors is usally a terrible idea. – Martin Gal Sep 02 '21 at 21:28
  • 1
    what do you mean by "another package"? If it's another R function, why not just put it in the for loop itself? – Robot-Scott Sep 02 '21 at 21:33
  • @MartinGal I am trying to get all of the column vectors individually using a loop. This way it is more efficient and then i can use each column vector individually – Gerald81645 Sep 02 '21 at 21:38
  • @Robot-Scott the other package requires vectors as input therefore i want to be able to input the columns into the package – Gerald81645 Sep 02 '21 at 21:40
  • There are possibilities using `assign` which is a [terrible idea](https://stackoverflow.com/questions/17559390/why-is-using-assign-bad). I highly doubt the "way more efficient" part. How is creating a new vector more efficient than using an existing object you can easily iterate over? If a function needs a vector as input, `some_function(df[, 1])` will work and you could use this in a loop: `for (i in seq_along(nrow(df))) { some_function(df[, i]) }`. – Martin Gal Sep 02 '21 at 21:43
  • @chris86787, please consider Martin Gal 's candid advice. You already have all your desired vectors stored in an easily acessible list of vectors (also known as dataframe). It can not get simpler/more efficient than what you already have. – GuedesBF Sep 02 '21 at 23:47
  • 1
    We could tell you how to get all the desired vectors from the dataframe into your global environment, but we would then be teaching you how to make yourself miserable, that is why no one answered this yet. – GuedesBF Sep 02 '21 at 23:50

1 Answers1

1

As pointed out in the comments, a dataframe is already a list of vectors. You can access the vectors using double bracket syntax. So you don't need to store them in new variables. Like so:

# Subset iris data frame for illustration purposes
df <- iris[1:5, ]

# Create sample function to print out class and vector values
myfunction <- function(vct) {
  
  print(class(vct)) 
  print(vct)
}

# Iterate columns and pass to function
for (i in 1:ncol(df)) {
  
   myfunction(df[[i]])
  
}

# Results
# [1] "numeric"
# [1] 5.1 4.9 4.7 4.6 5.0
# [1] "numeric"
# [1] 3.5 3.0 3.2 3.1 3.6
# [1] "numeric"
# [1] 1.4 1.4 1.3 1.5 1.4
# [1] "numeric"
# [1] 0.2 0.2 0.2 0.2 0.2
# [1] "factor"
# [1] setosa setosa setosa setosa setosa
# Levels: setosa versicolor virginica


David J. Bosak
  • 1,386
  • 12
  • 22