Compare Values from Two Columns of Data Set - r

I'm new to R and keep trying to think of it like JS, but it's definitely nothing like it. I need to take each value in column 1 and compare it to it's counter part in column 2.
For example: If the fifth item in column 1 is "Subaru" I need to see what the fifth item in column 2 is. If in column 2 the zip code is 24153, do one thing, if it's 24060 do something else.
With JS I would just run a for loop and do something like
for(i; i < name.length; i++){
if(name[i] == "Subaru" && zipcode[i] == 24153){
do this
} else if (blah blah){
blah blah blah
}
}
But you can't use i to get the index of each column in R because in an R for loop i is the value...right?
I tried nesting for loops but I have 7,000+ items in the database and cycling through all those twice is killer, even with breaks because the inner loop always starts back at 1.
I tried also putting them in a list but for some reason that's not working. Here are the two methods I've tried so far.
names = data$model
zips = data$zipcode
mylist <- list()
mylist[[1]] <- names
mylist[[2]] <- zips
r = 0
while (r <= length(mylist[[1]])) {
print(mylist[[1]][r])
print(mylist[[2]][r])
if(!is.na(mylist[[1]][r])){
if(mylist[[1]][r] == "Subaru" & mylist[[2]][r] == "24153"){
print(mylist[[1]][r])
}
}
}
And the nested for loops
names = data$model
zips = data$zipcode
for (j in names) {
if(!is.na(j)){
for (k in zips) {
if(!is.na(k)){
if(j == "Subaru" & k == "24153"){
print(j)
break
} else if(j == "Subaru" & k == "24060"){
print(j)
break
} else if(j == "Subaru" & k == "24019"){
print(j)
break
}
}
}
}
}
Any tips or ideas are welcome. I feel like I'm looking at it the wrong way.

Yes your for loop is in more of a java format. Try something like this:
for(i in 1:nrow(data){
if(name[i] == "Subaru" & zipcode[i] == 24153){
foo
}else if{
bar
}}

Related

R loop checking condition X times and break if not met

I'd like to create kind of dependency between R and database and for that I'm trying to create a loop which is checking if a date in one column from a database is equal to today's date, if yes then run main statement, if not wait 5mins and try again (max 24 times) then break. I'm stucked with the latter, if someone could advice that would be helpful, thanks!
if(lubridate::date(table$db_date) == Sys.Date()){
print(1)
} else {
Sys.sleep(300)
# and repeat the loop 24 times until statement is TRUE, if not then break
}
If you have an upper limit for the number of iterations, you should use a for loop:
for (i in seq_len(24)) {
if(lubridate::date(table$db_date) == Sys.Date()){
print(1)
break
} else {
Sys.sleep(300)
}
}
Very primitive but maybe something like this:
i=1 # establish global variable
while (T) {
if(lubridate::date(table$db_date) == Sys.Date()){
print(1)
# break
}
if(i < 24 & lubridate::date(table$db_date) != Sys.Date()) { # check both statements
i <<- i+1 # update global variable
Sys.sleep(0.1) # and repeat the loop 24 times until statement is TRUE, if not then break
}
else{break}
}

Loop command only consist of last value

I am trying to read.csv a series of file named ("year".csv) from year 2005 to 2007 and writing into database.
However the loop command only consist of "2007L" for element "i" and only read 2007.csv into the dataframe.
Any ideas on how to resolve the issue ?
for(i in c(2005:2007)){
ontime<-read.csv(paste0(i,".csv"), header=TRUE)}
if(i == 2005){
dbWriteTable(conn,"ontimet",ontimet)
} else {
dbWriteTable(conn,"ontimet",ontimet, append= TRUE)
}
}
Something like this might be what you need. I think you just need to assign variables and append in the right place.
You probably need is like this. It produces all three years (or in your case, it would be csv's)
tmp = NULL
for(i in c(2005:2007)){
if(i == 2005){
tmp = append(tmp, i)
} else {
tmp = append(tmp, i)
print(tmp)
}
}

confunsion in create if in R

I have a question how to make a IF
for (i in 1:12){
for (j in 1:12) {
if (i != j) {
var = x + b
}
else{ }
}}
"else" I need that when they are equal to continue with j + 1 example: if i = 4 and j = 4 then continue with j = 5 and continue counting until the end of j and continue the process of when i! = j
I think you don't understand what is going on in your code or you don't understand what for loops do. One "trick" you can do is to actually print what happens in your for loops so that you will have one idea of what is going on. You could also do this with a piece of paper.
As they already pointed you out, you don't need the else because the for already takes care of this.
for (i in 1:12){
print("-------------------------------")
valueI <- paste0("my i value is ",i)
print(valueI)
for (j in 1:12) {
valueJ <- paste0("my j value is ",j)
print(valueJ)
if (i != j) {
#var = x + b
diff <- paste0(i, " is different than ", j)
print(diff)
}
else{
}
}
}
This code is the same as yours and will generate a log that explains you what happens step from step, you could also use a debugger but seeing your struggles, better use this for now. What are you trying to calculate? I feel like you want to calculate the power of something...

Multiple conditions in if statements in R

I am trying to cut down a list of gene names that I have been given. I'm trying to eliminate any repetitive names that may be present but I keep getting an error when running my code:
counter=0
i=0
j=0
geneNamesRevised=array(dim=length(geneNames))
for (i in 0:length(geneNamesRevised))
geneNamesRevised[i]=""
geneNamesRevised
for (i in 1:length(geneNames))
for (j in 1:length(geneNamesRevised))
if (geneNames[i]==geneNamesRevised[j])
{
break
}
else if ((j==length(geneNamesRevised)-1) &&
(geneNames[i]!=geneNamesRevised[j]))
{
geneNamesRevised[counter]=geneNames[i]
counter++
}
The error message is a repetitive string of :
the condition has length > 1 and only the first element will be usedthe condition has length > 1 and only the first element will be usedthe condition has length > 1 and only the first element will be used
and this error message is for the last "else if" statement that has the '&&'.
Thank you!
Why not just
geneNamesRevised <- unique( geneNames )
... which returns a shortened list. There is also a duplicated function that can be used to remove duplicates when negated.
There are a few problems in your code.
1) The else is incorrectly specified - or not :) thanks #Mohsen_Fatemi
2) & is usually what you need rather than &&
3) counter++ isn't R
Copy the code below and see if it runs
for (i in 1:length(geneNames)){
for (j in 1:length(geneNamesRevised)){
if (geneNames[i]==geneNamesRevised[j])
{
break
} else {
if ((j==length(geneNamesRevised)-1) & (geneNames[i]!=geneNamesRevised[j]))
{
geneNamesRevised[counter]=geneNames[i]
counter <- counter + 1
}
}
}
}
Edit
4) also you were missing braces for your fors
use & instead of && ,
else if ((j==length(geneNamesRevised)-1) & (geneNames[i]!=geneNamesRevised[j]))

How to setup a recursive lapply for specific values ex.w[i] == n[i]?

Background
I'm developing a function that takes in a value for w between 1 and 3 and returns n values from one of 3 distributions.
The problem I am having is when n or w are not of length 1. So I've added 2 parameters nIsList and wIsList to create the functionality I want. The way I want this to work is as follows:
(Works as needed)
If nIsList ex( c(1,2,3) ) return a list equivalent to running consume(w,1), consume(w,2), consume(w,3)
(Works as needed)
If wIsList ex( c(1,2,3) ) return a list equivalent to running consume(1,n), consume(2,n), consume(3,n)
(Doesn't work as needed)
If nIsList ex(1,2,3) and wIsList ex(1,2,3)
return a list equivalent to running consume(1,1), consume(2,2), consume(3,3). Instead, I get a list equivalent to running [consume(1,1), consume(1,2), consume(1,3)], [consume(2,1), consume(2,2), consume(2,3)], [consume(3,1),consume(3,2), consume(3,3)]
I understand why I am getting the results I am getting. I just can't seem to figure out how to get the result I want. (As explained above)
Question
I want the function to provide a list for each element in w and n that is consume(w[i], n[i]) when wIsList & nIsList are True. Is there a way to do that using lapply?
The code:
library("triangle")
consume <- function(w, n=1, nIsList=F, wIsList=F){
if(!nIsList & !wIsList){
if(w==1){
return(rtriangle(n,0.3,0.8))
}else if(w==2){
return(rtriangle(n,0.7,1))
}else if(w==3){
return(rtriangle(n,0.9,2,1.3))
}
}
else if(nIsList & !wIsList){
return(sapply(n, consume, w=w))
}
else if(nIsList & wIsList){
return(lapply(n, consume, w=w, wIsList=T))
}
else if(!nIsList & wIsList){
return(lapply(w, consume, n))
}
}
Note: I am having trouble summarizing this question. If you have any suggestions for renaming it please let me know and I will do so.
Thanks to JPC's comment, using mapply does the trick. The new code is as follows:
consume <- function(w, n=1){
nIsList <- length(n) > 1 # Change based on JPC's second comment
wIsList <- length(w) > 1 # Change based on JPC's second comment
if(!nIsList & !wIsList){
if(w==1){
return(rtriangle(n,0.3,0.8))
}else if(w==2){
return(rtriangle(n,0.7,1))
}else if(w==3){
return(rtriangle(n,0.9,2,1.3))
}
}
else if(nIsList & !wIsList){
return(sapply(n, consume, w=w))
}
else if(nIsList & wIsList){
return(mapply(consume,w,n)) ## Updated portion
}
else if(!nIsList & wIsList){
return(lapply(w, consume, n))
}
}

Resources