87922

+ signs appearing in console in R

I´m quite new at programming, and I started with R. I wanted to make a script which takes as input a matrix with the columns: time, x, y (coordenates) and then divide the time spent in each quadrant of a circular area. I have a roughly done script, but for any reason I don´t understand, when I execute the for loop in the editor, it appears all the letters red in the console with a "+" at the beginning of each line, which doesn´t let me open any other object any more when I call it.

set.seed(20) x = matrix(rnorm(10,3,4),10,1) y = matrix (rnorm(10,3,4),10,1) time = matrix (1:10,10,1) a = cbind (time,x,y) colnames(a) = c("time","x","y") upperright = a[,2] > 0 && a[,3] > 0 upperleft = a[,2] < 0 && a[,3] > 0 lowerright = a[,2] > 0 && a[,3] < 0 lowerleft = a[,2] < 0 && a[,3] < 0 calculation <- function () { upperrightquadrant = 0 upperleftquadrant = 0 lowerrightquadrant = 0 lowerleftquadrant = 0 for (i in c(1:nrow(a))){ if (upperright) upperrightquadrant <<- sum((a[i,1]-a[i-1,1])) if (upperleft)upperleftquadrant <<- sum((a[i,1]-a[i-1,1])) if (lowerright) lowerrightquadrant <<-sum((a[i,1]-a[i-1,1])) if (lowerleft) lowerleftquadrant <<- sum((a[i,1]-a[i-1,1])) }} output = lapply (a, calculation)

Answer1:

The + symbols are only a way to show that the current statement is conitnuation of the previous lines's statement. That's perfectly OK. There are a couple of other problems with your program which are stopping it from producing your intended result. If I have in fact, understood what you are trying to do, I have added a possible solution at the end. First, let me suggest the edits and the reason for them:

<ol> <li>& should have been used instead of &&. & is the vectorized operator. So, upperright = a[,2] > 0 & a[,3] > 0 instead of upperright = a[,2] > 0 && a[,3] > 0.</li> <li>You are passing a value to the function so its definition should include the parameter calculation <- function (a) instead of calculation <- function ().</li> <li>upperright, upperleft, ... are vectors and you should check individual elements instead of the whole vector if (upperright[i]) instead of if (upperright).</li> <li>Problem with sum((a[i,1]-a[i-1,1])) at i==1, i-1 equals 0. If you are only counting, incrementing the count by 1 should be sufficient.</li> <li>The objects upperrightquadrant, upperleftquadrant, ... are in your function environment only. So, no need for <<-.</li> <li>

Finally, as you have already written the for loop to iterate over your matrix rowwise, no need for lapply or other similar looping functions.

Only a suggestion: Use <- operator for assignments instead of =.

</li> </ol>

Now, the corrected program:

set.seed(20) x = matrix(rnorm(10,3,4),10,1) y = matrix (rnorm(10,3,4),10,1) time = matrix (1:10,10,1) a = cbind (time,x,y) colnames(a) = c("time","x","y") upperright = a[,2] > 0 & a[,3] > 0 upperleft = a[,2] < 0 & a[,3] > 0 lowerright = a[,2] > 0 & a[,3] < 0 lowerleft = a[,2] < 0 & a[,3] < 0 calculation <- function (a) { upperrightquadrant = 0 upperleftquadrant = 0 lowerrightquadrant = 0 lowerleftquadrant = 0 for (i in c(1:nrow(a))){ if (upperright[i]) upperrightquadrant <- upperrightquadrant + 1 if (upperleft[i]) upperleftquadrant <- upperleftquadrant + 1 if (lowerright[i]) lowerrightquadrant <- lowerrightquadrant + 1 if (lowerleft[i]) lowerleftquadrant <- lowerleftquadrant + 1 } list(upperrightquadrant = upperrightquadrant, upperleftquadrant = upperleftquadrant, lowerrightquadrant = lowerrightquadrant, lowerleftquadrant = lowerleftquadrant) } output = calculation(a) print(a) # input print(output) # output

EDIT

This is the edit for what you requested (Case where time instances are not contiguous). Just replace the for loop with this one:

for (i in c(1:nrow(a))){ if(i>1){ if (upperright[i]) upperrightquadrant <- upperrightquadrant + a[i, 1] - a[i-1, 1] if (upperleft[i]) upperleftquadrant <- upperleftquadrant + a[i, 1] - a[i-1, 1] if (lowerright[i]) lowerrightquadrant <- lowerrightquadrant + a[i, 1] - a[i-1, 1] if (lowerleft[i]) lowerleftquadrant <- lowerleftquadrant + a[i, 1] - a[i-1, 1] } else { if (upperright[i]) upperrightquadrant <- upperrightquadrant + a[i, 1] if (upperleft[i]) upperleftquadrant <- upperleftquadrant + a[i, 1] if (lowerright[i]) lowerrightquadrant <- lowerrightquadrant + a[i, 1] if (lowerleft[i]) lowerleftquadrant <- lowerleftquadrant + a[i, 1] } }

Hope, this helps.

Recommend

  • quick pandas groupby calculations with cumprod
  • Aggregating two data frame columns without any existing pattern logic
  • C# fibonacci function returning errors
  • Set matplotlib plot axis to be the dataframe column name
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • onBackPressed() not being executed
  • Passing 1 of 3 values to a Sub
  • debug library loaded with ctypes using gdb
  • How to install a .deb file on a jailbroken iphone programmatically?
  • How to use an array of arrays with array_map(…) in PHP?
  • How to rebase a series of branches?
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Ajax jQuery multiple calls at the same time - long wait for answer and not able to cancel
  • Date difference with leap year
  • Is possible to count alias result on mysql
  • Perl system calls when running as another user using sudo
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Hazelcast - OperationTimeoutException
  • Adding custom controls to a full screen movie
  • Rearranging Cells in UITableView Bug & Saving Changes
  • SVN: Merging two branches together
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • Load html files in TinyMce
  • How to set the response of a form post action to a iframe source?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Qt: Run a script BEFORE make
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Busy indicator not showing up in wpf window [duplicate]
  • reshape alternating columns in less time and using less memory
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Why do underscore prefixed variables exist?
  • java string with new operator and a literal