2

I am trying to perform calculations on a list of data frames using hierarchical criteria. IE preferably I use criteria 1, but if not, criteria 2 is fine. If the data frame doesn't satisfy either it goes to Criteria 3, etc.

I want to return 3 lists of 36 observations (nrow in my data frame) & filtered by my criteria. However, neither category 1 or category 2 come back with 36 observations. Why is this?

Here is a reproducible example:

#dummy data

d1 <- data.frame(a=rnorm(6), b=1:6, c=rnorm(6), d = rnorm(6), e = rnorm(6))
d2 <- data.frame(a=1:6, b=rnorm(6), c =rnorm(6), d = rnorm(6), e = rep(NA, 
times = 6))
d3 <- data.frame(a=1:6, b=rnorm(6), c= rnorm(1:6), d = rep(NA, times = 6), e 
= rnorm(6))
d4 <- data.frame(a=1:6, b=rnorm(6), c =rnorm(6), d = rnorm(6), e = rep(NA, 
times = 6))
d5 <- data.frame(a=rnorm(6), b=1:6, c=rnorm(6), d = rep(NA, times = 6), e = 
rnorm(6))
d6 <- data.frame(a=rnorm(6), b=1:6, c=rnorm(6), d = rep(NA, times = 6), e = 
rep(NA, times = 6))

my_test_data <- list(d1, d2, d3, d4, d5, d6)
test_data_rows <- rbind.fill(my_test_data)

#seperate into categories

Category1 <- list()
Category2 <- list()
Category3 <- list()

for (i in 1:NROW(test_data_rows)) {
if (!is.na(test_data_rows$e[i] == TRUE)) {
Category1[i] <- test_data_rows$e[i]*test_data_rows$c[i]*(test_data_rows$b[i] 
/ test_data_rows$a[i])
} else if (!is.na(test_data_rows$d[i] == TRUE) & is.na(test_data_rows$e[i]) 
== TRUE) {
Category2[i] <- (0.432 * test_data_rows$d[i] * (test_data_rows$b[i] / 
test_data_rows$a[i]))
} else if (is.na(test_data_rows$d[i] == TRUE) & is.na(test_data_rows$e[i] == 
TRUE)) {
Category3[i] <- (0.101 * 0.432*(test_data_rows$b[i] / test_data_rows$a[i]))
}
}

Thanks in advance for any help and advice you can give.

EDIT: I have managed to solve this. The position of the [i] next to the 'Categoryx' was wrong. Also there is no need to use multiple categories.

for (i in 1:NROW(test_data_rows)) {
if (!is.na(test_data_rows$e[i] == TRUE)) {
Category1[i] <- test_data_rows$e[i]*test_data_rows$c[i]*(test_data_rows$b[i] 
/ test_data_rows$a[i])
} else if (!is.na(test_data_rows$d[i] == TRUE) & is.na(test_data_rows$e[i]) 
== TRUE) {
Category1[i] <- (0.432 * test_data_rows$d[i] * (test_data_rows$b[i] / 
test_data_rows$a[i]))
} else if (is.na(test_data_rows$d[i] == TRUE) & is.na(test_data_rows$e[i] == 
TRUE)) {
Category1[i] <- (0.101 * 0.432*(test_data_rows$b[i] / test_data_rows$a[i]))
}
}
4
  • I have solved this after many hours frustration. There are some syntax errors above. Commented Dec 3, 2017 at 22:57
  • 1
    Rather than edit the answer in to your question, post it as an answer. After 24 hours you can "accept" it which will mark the question as resolved. Commented Dec 3, 2017 at 23:08
  • Sorry new to the site Commented Dec 3, 2017 at 23:16
  • No worries! Good job figuring out your own problem, and thanks for following up with a solution! Commented Dec 3, 2017 at 23:19

1 Answer 1

3

VICTORY! I have managed to solve this. The position of the [i] next to the 'Categoryx' was wrong. Also there is no need to use multiple categories.

for (i in 1:NROW(test_data_rows)) {
if (!is.na(test_data_rows$e[i] == TRUE)) {
Category1[i] <- test_data_rows$e[i]*test_data_rows$c[i]*(test_data_rows$b[i] 
/ test_data_rows$a[i])
} else if (!is.na(test_data_rows$d[i] == TRUE) & is.na(test_data_rows$e[i]) 
== TRUE) {
Category1[i] <- (0.432 * test_data_rows$d[i] * (test_data_rows$b[i] / 
test_data_rows$a[i]))
} else if (is.na(test_data_rows$d[i] == TRUE) & is.na(test_data_rows$e[i] == 
TRUE)) {
Category1[i] <- (0.101 * 0.432*(test_data_rows$b[i] / test_data_rows$a[i]))
}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.