0

Here is my data in tab delimited txt file:

https://drive.google.com/open?id=1SfqY8FBnpLCs8TKySK_HYR6kzWChLRjb

Is there a way to make a for loop for the following code:

ka1 <- dput(as.numeric(na.omit(data$Ka1)))

ka2 <- dput(as.numeric(na.omit(data$Ka2)))

ka3 <- dput(as.numeric(na.omit(data$Ka3)))

reference <- dput(as.numeric(na.omit(data$Reference)))

ks1 <- dput(as.numeric(na.omit(data$Ks1)))

ks2 <- dput(as.numeric(na.omit(data$Ks2)))

ks3 <- dput(as.numeric(na.omit(data$Ks3)))

I am trying to place each individual column of the df into a list assigned to a vector:

> Reference
[1] 1401

> Ka1
[1]  57 108 333 510

I Tried the following, but I do not understand the entire process.

for (i in data) {
  names(data) <- deput(as.numeric(na.omit(data$i)))
}
  • 1
    Please share a [minimal, reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) of your data. – kath Mar 02 '18 at 11:02
  • You'r example data can not be coerced to data.frame due to different number of rows. Also it's very unclear of what your desired result is or what you attempt to do. – Aleh Mar 02 '18 at 11:23

2 Answers2

0

Something like this:

for(var in names(data)) {
    data[[var]] <- dput(as.numeric(na.omit(data[[var]])))
}
Aleh
  • 776
  • 7
  • 11
0

To extract the columns of you data into a list you can do the following:

data_list <- lapply(data, function(x) as.numeric(na.omit(x)))

data_list[["Ka1"]]
[1]  57 108 333 510

This is a short and faster version of the following for loop:

data_list <- vector(length = ncol(data), mode = "list")
names(data_list) <- names(data)

for (i in names(data)) {
  data_list[[i]] <- as.numeric(na.omit(data[[i]]))
}
kath
  • 7,624
  • 17
  • 32