so I have a dataframe with four number colums A,B,C and D
I now wrote a for-loop to change values row by row when certain conditions are met.
but nothing happens! If I manually set k=1, k=2, k=3 and run the two if functions in the loop manually it works! This seems like a bug to me?
for (k in nrow(list)) {
if(list$A[k]>list$C[k]) {list$C[k]=list$A[k]}
if(list$B[k]<list$D[k]) {list$D[k]=list$B[k]}
}
list
Example:
A B C D
195679832 197768053 195000001 197500000
195679832 197768053 197500001 200000000
227573015 228592110 227500001 230000000
64199445 65230121 65000001 67500000
should become
A B C D
195679832 197768053 195679832 197500000
195679832 197768053 197500001 197768053
227573015 228592110 227573015 228592110
64199445 65230121 64199445 65000000
64199445 65230121 65000001 65230121
again: when I manually increase k and run the content of the loop one by one it works just fine, but if i run the loop as a whole it wont execute the if clauses anymore. It gives no error message aswell.
You forgott the 1: before nrow(list). k was only receiving 4, not 1:4.
for (k in 1:nrow(list)) {
if(list$A[k]>list$C[k]) {list$C[k]=list$A[k]}
if(list$B[k]<list$D[k]) {list$D[k]=list$B[k]}
}
list
Related
I have an array 'A'. I want to do a loop over all the elements of A, checking to see if any are greater than or equal to 1. If they are, I would like to assign a '1' to a new array 'B' in the same element index of A.
How would I go about implementing this?
I have the cumbersome idea of:
for i in 1:end
for j in 1:end
if A[i,j] >= 1
B[i,j] = 1
else
B[i,j] = 0
end
end
end
but I would prefer something more succinct.
Just use broadcast:
B = A .≥ 1
You can certainly use broadcasting as Oscar suggested (e.g. B = A .>= 1), but there's also nothing wrong with loops, since loops are fast and avoid excess allocations. You really only need one loop though, and the if statement is slightly superfluous, so:
B = similar(A, Int64) # If B doesn't already exist, otherwise omit this line
#inbounds for i in eachindex(A)
B[i] = A[i] >= 1
end
The #inbounds is optional, but improves speed.
I have a table "weather". I insert weather conditions for a particular day. I can't seem to write a function that prints the contents of "weather" (see below for things I've tried.
day = "Friday"
conditions = {"Sunny", "85", "windy"}
weather = {{}} --nested table
for k, v in pairs(conditions) do
weather[day] = {[k]=v}
end
I've tried two things to print the weather table and neither work.
for k, v in pairs(weather) do
print(k, v)
end
---- Output ---
1 table: 0x2542ae0
Friday table: 0x25431a0
This doesn't work either, but I thought it would
for k, v in pairs(weather) do
for l, w in pairs(v) do
print(l, w)
end
end
----Output----
3 windy
You are overwriting weather[day] in the first loop and so only the last value remains.
I think you want simply this, instead of that loop:
weather[day] = conditions
I want to print a specified element from each list until it stops, however I am not sure of what loop conditions to use.
Code
For example, I have printed element[0] of each list but I want it to loop until it prints all the elements.
Any help is appreciated.
Thanks
The following code is, maybe, the simplest solution to print all the cats. If you want to print a single list, use only the last 2 lines after replacing "l" with your list variable.
a = ['Ginger', 'Lion', 'Persian', 'Sphynx']
b = ['Mainecoon', 'Tiger', 'Ragdoll', 'Birman']
c = ['Lynx', 'Shorthair', 'Abyssinian', 'Scottishfold']
d = ['Ocelot', 'Jaguar', 'Chartreux', 'Korat']
lists = [a, b, c, d]
for l in lists:
for cat in l:
print(cat)
I have been given a problem to solve that I am fairly certain its insoluble.
For a system I am working in I need to take piece of branching logic (graph) and translate it to a linear path(flatten it), without node repeats. Given a tree I know that I can do this.
The rules are that the path must be traversed in order, but can 'skip' any panel if some condition is met.
Given the tree:
A > B > C
&&
A > D > E
Our tree can be flattened to:
A > B > C > D > E
So in this case B and C share the same conditional, and D, and E have the inverse of that condition. Thus if B is met so is C, but D and E will be skipped. Conversely, if B is not met, B and C are skipped, but D and E aren't.
So far, so simple. I am fairly convinced this is true for any tree. The problem I have is that the objects I have been given to flatten are graphs, and contain simple cycles, and closed walks.
After that huge preamble my questions are:
Am I right in stating that it is impossible to guarantee that such a graph can be flattened?
I know that closed walks cannot follow my rules (by virtue of returning to a node), but are there any other rules that describe a 'flatten-able' graph versus a 'non-flatten-able' one?
Cheers
If it is a directed graph, then the graph can be flattened if there are no directed cycles.
There is a handy library available JGraphT which will provide the necessary methods to determine if there is a cycle presnet as well a TopologicalOrderIterator which will perform the flattening as well.
Using the graph library and the example above this code will show an exmaple of how to do it.
DirectedGraph<String, DefaultEdge> graph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("C");
graph.addVertex("D");
graph.addVertex("E");
// A > B
graph.addEdge("A", "B");
// B > C
graph.addEdge("B", "C");
// A > D
graph.addEdge("A", "D");
// D > E
graph.addEdge("D", "E");
// Uncomment the following line to create a cyclic graph.
//graph.addEdge("E", "D");
CycleDetector<String, DefaultEdge> cycleDector = new CycleDetector<String, DefaultEdge>(graph);
if (cycleDector.detectCycles()) {
System.err.println("Cyclic graph");
} else {
StringBuilder sb = new StringBuilder();
// Create topological order iterator
for (TopologicalOrderIterator<String, DefaultEdge> iter = new TopologicalOrderIterator<String, DefaultEdge>(
graph); iter.hasNext();) {
String vertex = iter.next();
if (sb.length() > 0) {
sb.append(" > ");
}
sb.append(vertex);
}
System.out.println(sb.toString());
}
Sample output with the cycle disabled.
A > B > D > C > E
I would like to write an if statement of the following form:
a=5
b=2
la<-function(a,b){
if(a>3){a}
else{b}
}
Now what I would like to do is not only have one action in the if statement but two, for example:
if(a>3){a and c<<-1000}
In this case to return 'a' and also write 1000 in variable 'c'
My question is how to put in multiple actions after the if statement.
You should use the semicolon
if(a>3){c<-1000;a}
The last statement is the return value.
EDIT This works for multiple statements, too. You can omit the semicolon if you use line breaks, as in
if(a>3) {
c<-1000
d<-1500
a
} else {
e <- 2000
b
}
EDIT: this should work with ifelse too. You would have to keep in mind that you are operating on a vector, though. Here is an example:
x <- sample(10, 100, replace=TRUE)
ifelse(x>3, {y <- 100; y+x}, x)
This adds 100 only to those elements of x that are larger than 3.