Need to Print Value of a Variable using Paste in R - r

I am trying to create a data frame of various error messages based on Data to be cross checked between two dataframes and storing the message in a vector in an iterative manner . I am using the following snippet for this purpose :
> for(j in 1:nrow(MySQL_Data)){ date_mysql=
> paste("MySQL_Data[",j,",1]") date_red= paste("RED_Data[",j,",1]")
> body= c() if(!date_mysql == date_red) {
> body<- append(body,paste("'There is data missing for date",date_mysql,"in",table2)) }else {
> NULL }}
My table2 variable prints as MYSQL_Data[2,1] instead of the actual value of the variable which is a date
Following is the Output :
"'There is data missing for date MySQL_Data[ 2 ,1] in Dream11_UserRegistration"
Can someone help me with the error that I am committing here..
Thanks in Advance !

Your use of paste in the definitions of data_mysql and data_red makes no sense. I’m assuming that what you actually want is this:
data_mysql = MySQL_Data[j, 1]
data_red = RED_Data[j, i]
Furthermore, you’re resetting body in every loop iteration so it will only ever hold a single element.

Related

Length Zero in r

Greetings I am getting an error of
Error in if (nrow(pair) == 0) { :argument is of length zero
I have checked the other answers but do not seem to work on a variable like mine. Please check code below, please assist if you can.
pair<-NULL
if(exists("p.doa.ym")) pair <- rbind(pair, p.doa.ym[,1:2])
if(exists("p.doa.yd")) pair <- rbind(pair, p.doa.yd[,1:2])
if(nrow(pair) == 0) {
print("THERE ARE NO MATCHES FOR TODAY. STOP HERE")
quit()
}
Since you set pair=NULL and then it might happen that pair stays null if those two if statements are not true, you either need to check if pair is null first, or you could set pair to an empty data frame, or something else.
One option:
if (!is.null(pair)) {
if (nrow(pair)==0) {
# your code
}
}
Another option:
pair=data.frame()
# your code

How to solve this error of conditional statements and Loops in R?

You can suggest me any sort of answers, not necessarily need using conditional statements and Loops.
I have the data set with several ids and and three alert or groups.
here is the image for the concept:
and here is the actual Data set for one ID: Click me
Concept is:
I have three alert: Relearn - Rebuild - Replace.
and after relearn: rebuild or replace can come but relearn cannot come
and after rebuild: replace can come but relearn cannot come
after replace: relearn and rebuild cannot come. if there is any replace only that can come
I have attached the image and Dataset for better clear understanding and Here is my try:
temp1 = NULL
temp2 = NULL
sql50 = NULL
for(i in 1:nrow(BrokenPins)) #First Loop
{
sql50 = sqldf(paste("select * from rule_data where key = '",BrokenPins[i,1],"'",sep = ""))
for(j in 1:nrow(sql50))
{ #Second Loop
while (head(sql50$Flag,1) == sql50$Flag[j] )
{
temp1 = sql50[j,]
temp2 = rbind(temp2,temp1)
print("Send")
if(j == 1 || sql50$Flag[j] == sql50$Flag[j-1])
{
j = j+1
}
else(sql50$Flag[j] > sql50$Flag[j-1])
{
break
}
}
}
}
First loop will go through each id and second loop will give me all the alert for that id.
so in the image i have added send and dont send. it wont be in actual table. that basically means send means copy it to new dataframe like i am doing above rbind in the code and dont send means dont copy it. This Above piece of code will run but only take the first and end it.
Finally, Based On above Data set Click me: that is for one ID (key), Flag (1 - Relearn, 2-Rebuild,3-replace). so based on this dataset. my output should have Row 1, 2 and 7 because First Relearn[Flag 1] came then Rebuild[Flag 2] then again relearn[Flag 1]cannot come, only rebuild [Flag 2] and replace[Flag 2] can.
can you help me solve this concept?
One thing I notice is that you use else and also provided a condition; you should only use else when you want every case that is not included in the condition of your if statement. Basically, instead of using else(sql50$Flag[j] > sql50$Flag[j-1]) you should be using else if(sql50$Flag[j] > sql50$Flag[j-1]).

Need assistance with understanding R for loop error: unexpected '}' in " }"

Just to give some background first:
I currently have 2 data frames (giraffe, leaf) and both of them share the column 'key', where the elements in the leaf data frame are a subset of giraffe. What I needed to do is compare the two data frames and when there are matching elements in both data frames in the 'key' column, the string 'leaf' will be input into another column (project) in the giraffe data frame inside the same row as the matching 'key' element. I've taken the following approach however it seems I have made a small error somewhere and after searching online, I still don't know what it is:
Truth_vector <- is.element((giraffe[,1]),(leaf[,1])) #returns a vector with 3000 elements, most are FALSE except for where the element inside 'key' is present in both data frames
i=1
for (i in 1:length(giraffe[,1])) {
if Truth_vector[i] == TRUE {
giraffe[i,5] <- 'leaf'
}
i = i+1
}
Error: unexpected '}' in "}"
Edit:
I tried implementing the solution as a function however nothing ends up happening, no error messages get returned either. What I've done is:
Project_assign <- function(prjct) {
Truth_vector <- is.element((giraffe[,1]),(prjct[,1]))
giraffe[which(Truth_vector),5] <- 'prjct'
}
Project_assign(leaf)
Edit: This was because everything was getting assigned in the function sub environment, not the global environment. Using assign('giraffe',giraffe,envir=.GlobalEnv) solves this however you should try and avoid the assign function and Instead I used a for loop going over a list of all the dataframes
You have a couple issues. First, the if criteria needs to be in parentheses, and secondly you don't need to increment i yourself. This should suffice:
for (i in 1:length(giraffe[,1])) {
if (Truth_vector[i] == TRUE) {
giraffe[i,5] <- 'leaf'
}
}
Of course, this would do it too:
giraffe[which(Truth_vector),5] <- 'leaf'
(assuming Truth_vector is not longer than the number of rows in giraffe)

Error: missing value where True/False

I am trying to delete all values in a list that have the tag ".dsw". My list is a list of files using the function list.files. This is my code:
for (file in GRef) {
if (strsplit(file, "[.]")[[1]][3] == "dsw") {
#GRef=GRef[-file]
for(n in 1:length(GRef)){
if (GRef[n] == file){
GRef=GRef[-n]
}
}
}
}
Where GRef is the list of file names. I get the error listed above, but I dont understand why. I have looked at this post: Error .. missing value where TRUE/FALSE needed, but I dont think it is the same thing.
You shouldn't attempt to to modify a vector while you are looping over it. The problem is your are removing items you are then trying to extract later which is causing the missing values. It's better to identify all the items you want remove first, then remove them. For example
GRef <- c("a.file.dsw", "b.file.txt", "c.file.gif", "d.file.dsw")
exts <- sapply(strsplit(GRef, "[.]"), `[`, 3)
GRef <- GRef[exts!="dsw"]

If inside of double loop

Inside of double loop I create subset of data for each combination of Tv stations with months which is done by loops. For example I have monthsnumbers 7,8,9 and stations A,B,C. It happens that for Month 9 there is no station C.
Then subset is empty and function throws and error of no possible aggregation.
So as you can see I tried to use if statement that if there are 0 rows don't continue with the code but go on to the next loop.
But I still get the same error fck. message
can you please navigate me ?
for (Mesic in monthnumbers){
for (Stanica in TVstations){
Client<-data[data$month ==Mesic & data$Channel_group1 ==Stanica & data$Brand == brand, ]
if (nrow(Client)!=0)
###some code
Client_AGG<-aggregate(formula= Client$BUYING_GRPs ~ Client$Brand,data= Client,FUN = sum)
###some code
}
}
}
Like Gregor commented, there is likely a better way of going about this.
But a quick patch might be to put the error check before the Client<- line because that's where it's looking for a channel that doesn't exist. Check if data$Channel_group1 == Stanica even exists before trying to get data from it.
Another option using for loops is to cycle through what you know is there with something like this:
subsetindex <- unique(data[ ,c('month','Channel_group1')])
for(i in 1:nrow(subsetindex)){
Client<-data[data$month ==subsetindex[i,'month'] & data$Channel_group1 ==subsetindex[i,'Channel_group1'] & data$Brand == brand, ]
#other code
}

Resources