11 -> 3
10 -> 3.1
9 -> 3.333
8 -> 3.5
7 -> 3.7142857142857
6 -> 4
5 -> 4.4
4 -> 5
3 -> 5.666
2 -> 7
1 -> 10
Basically I'm trying to reverse engineer a function for calculating Xp awarded to a player. The first number is what you feed into the function, while the second number is what it returns. After visualizing the returned numbers I figured out that they're an inverse exponential, but I've had no luck in implementing them in lua.
for n = 1, 11 do
local xp = math.floor(10 * n^.5)/n
print(n, xp)
end
Output:
1 10
2 7
3 5.6666666666667
4 5
5 4.4
6 4
7 3.7142857142857
8 3.5
9 3.3333333333333
10 3.1
11 3
Related
I have a dataframe, with a column that increases with every row, and periodically (though not regularly) resets back to 1.
I'd like to track/ count these resets in separate column. This for-loop example does exactly what I want, but is incredibly slow when applied to large datasets. Is there a better/ quicker/ more R way to do this same operation:
ColA<-seq(1,20)
ColB<-rep(seq(1,5),4)
DF<-data.frame(ColA, ColB)
DF$ColC<-NA
DF[1,'ColC']<-1
#Removing line 15 and changing line 5 to 1.1 per comments in answer
DF<-DF[-15,]
DF[5,2]<-0.1
for(i in seq(1,nrow(DF)-1)){
print(i)
MyRow<-DF[i+1,]
if(MyRow$ColB < DF[i,'ColB']){
DF[i+1,"ColC"]<-DF[i,"ColC"] +1
}else{
DF[i+1,"ColC"]<-DF[i,"ColC"]
}
}
No need for a loop here. We can just use the vectorized cumsum. This ought to be faster:
DF$ColC<-cumsum(DF$ColB==1)
DF
To keep using varying variable reset values that are always lower then the previous value, use cumsum(ColB < lag(ColB)):
DF %>% mutate(ColC = cumsum(ColB < lag(ColB, default = Inf)))
ColA ColB ColC
1 1 1.0 1
2 2 2.0 1
3 3 3.0 1
4 4 4.0 1
5 5 0.1 2
6 6 1.0 2
7 7 2.0 2
8 8 3.0 2
9 9 4.0 2
10 10 5.0 2
11 11 1.0 3
12 12 2.0 3
13 13 3.0 3
14 14 4.0 3
16 16 1.0 4
17 17 2.0 4
18 18 3.0 4
19 19 4.0 4
20 20 5.0 4
I want to create a rectangular square lattice using GraphViz where all nodes are connected in both directions to their neighbors. The problem is, that if I use the terminal comand
osage -Tpng graph.gv > graph.png
to create this rectangular graph, the first and the last nodes are swapped. I checked all the links between the nodes and changing the position of node 0 and node 15 would yield the desired structure with correct connections. Here a small example of the resulting graph:
This issue also remains if I cancel all node-connections and only use the simple graph.gv file:
graph G {
0;
1;
2;
3;
4;
5;
6;
7;
8;
}
Up to 6 nodes, the node ordering is correct without any swap. For any higher node number, it gets mixed. I would expect osage to automatically correct the wrong ordering while adding the links between the nodes. But adding links does not change anything in the wrong ordering. I have also looked at possible attributes but could not find anything which would solve the problem.
Has anybody experienced a similar problem and can help me with this issue? Thanks in advance!
This seems to have been fixed. This input
digraph grid {
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
0 -> 1
0 -> 4
1 -> 0
1 -> 2
1 -> 5
2 -> 1
2 -> 3
2 -> 6
3 -> 2
3 -> 7
4 -> 5
4 -> 0
4 -> 8
5 -> 4
5 -> 6
5 -> 1
5 -> 9
6 -> 5
6 -> 7
6 -> 2
6 -> 10
7 -> 6
7 -> 3
7 -> 11
8 -> 9
8 -> 4
8 -> 12
9 -> 8
9 -> 10
9 -> 5
9 -> 13
10 -> 9
10 -> 11
10 -> 6
10 -> 14
11 -> 10
11 -> 7
11 -> 15
12 -> 13
12 -> 8
13 -> 12
13 -> 14
13 -> 9
14 -> 13
14 -> 15
14 -> 10
15 -> 14
15 -> 11
}
And osage from Graphviz version 2.43.0 gives:
I have a data with primary key and ratio values like the following
2.243164164
1.429242413
2.119270714
3.013427143
1.208634972
1.208634972
1.23657632
2.212136028
2.168583297
2.151961216
1.159886063
1.234106444
1.694206176
1.401425329
5.210125578
1.215267806
1.089189869
I want to add a rank column which groups these ratios in say 3 bins. Functionality similar to the sas code:
PROC RANK DATA = TAB1 GROUPS = &NUM_BINS
I did the following:
Convert your vector to data frame.
Create variable Rank:
test2$rank<-rank(test2$test)
> test2
test rank
1 2.243164 15.0
2 1.429242 9.0
3 2.119271 11.0
4 3.013427 16.0
5 1.208635 3.5
6 1.208635 3.5
7 1.236576 7.0
8 2.212136 14.0
9 2.168583 13.0
10 2.151961 12.0
11 1.159886 2.0
12 1.234106 6.0
13 1.694206 10.0
14 1.401425 8.0
15 5.210126 17.0
16 1.215268 5.0
17 1.089190 1.0
Define function to convert to percentile ranks and then define pr as that percentile.
percent.rank<-function(x) trunc(rank(x)/length(x)*100)
test3<-within(test2,pr<-percent.rank(rank))
Then I created bins on the fact you wanted 3 of them.
test3$bins <- cut(test3$pr, breaks=c(0,33,66,100), labels=c("0-33","34-66","66-100"))
test x rank pr bins
1 2.243164 15.0 15.0 88 66-100
2 1.429242 9.0 9.0 52 34-66
3 2.119271 11.0 11.0 64 34-66
4 3.013427 16.0 16.0 94 66-100
5 1.208635 3.5 3.5 20 0-33
6 1.208635 3.5 3.5 20 0-33
7 1.236576 7.0 7.0 41 34-66
8 2.212136 14.0 14.0 82 66-100
9 2.168583 13.0 13.0 76 66-100
10 2.151961 12.0 12.0 70 66-100
11 1.159886 2.0 2.0 11 0-33
12 1.234106 6.0 6.0 35 34-66
13 1.694206 10.0 10.0 58 34-66
14 1.401425 8.0 8.0 47 34-66
15 5.210126 17.0 17.0 100 66-100
16 1.215268 5.0 5.0 29 0-33
17 1.089190 1.0 1.0 5 0-33
That work for you?
Almost late but given your data, we can use ntile from dplyr package to get equal sized groups:
df <- data.frame(values = c(2.243164164,
1.429242413,
2.119270714,
3.013427143,
1.208634972,
1.208634972,
1.23657632,
2.212136028,
2.168583297,
2.151961216,
1.159886063,
1.234106444,
1.694206176,
1.401425329,
5.210125578,
1.215267806,
1.089189869))
library(dplyr)
df <- df %>%
arrange(values) %>%
mutate(rank = ntile(values, 3))
values rank
1 1.089190 1
2 1.159886 1
3 1.208635 1
4 1.208635 1
5 1.215268 1
6 1.234106 1
7 1.236576 2
8 1.401425 2
9 1.429242 2
10 1.694206 2
11 2.119271 2
12 2.151961 2
13 2.168583 3
14 2.212136 3
15 2.243164 3
16 3.013427 3
17 5.210126 3
Or see cut_number from ggplot2 package:
library(ggplot2)
df$rank2 <- cut_number(df$values, 3, labels = c(1:3))
values rank rank2
1 1.089190 1 1
2 1.159886 1 1
3 1.208635 1 1
4 1.208635 1 1
5 1.215268 1 1
6 1.234106 1 1
7 1.236576 2 2
8 1.401425 2 2
9 1.429242 2 2
10 1.694206 2 2
11 2.119271 2 2
12 2.151961 2 3
13 2.168583 3 3
14 2.212136 3 3
15 2.243164 3 3
16 3.013427 3 3
17 5.210126 3 3
Because your sample consists of 17 numbers, one bin consists of 5 numbers while the others consist of 6 numbers. There are differences for row 12: ntile assigns 6 numbers to the first and second group, whereas cut_number assigns them to the first and third group.
> table(df$rank)
1 2 3
6 6 5
> table(df$rank2)
1 2 3
6 5 6
See also here: Splitting a continuous variable into equal sized groups
I have a data frame, df, that looks this this:
a b c d e f g h i j k l m
1a 4 4 3 4 3 4 3 4.0 4.0 4 4 4 3.9
1b 9 9 9 9 9 9 9 8.1 8.8 9 9 9 8.5
1c 8 8 9 8 9 8 8 8.0 9.0 8 9 8 8.3
1d 8 8 8 9 8 9 8 8.0 8.0 8 8 8 8.5
1e 4 4 4 4 4 4 4 4.0 4.0 4 4 4 4.0
2a 3 4 3 4 3 4 3 4.0 3.0 4 3 4 3.8
2b 8 8 8 8 8 8 8 8.0 8.0 8 8 8 8.0
2c 8 8 8 8 8 8 8 9.0 8.0 9 8 8 8.3
2d 8 9 8 8 8 9 8 9.0 8.0 9 8 9 8.0
2e 4 3 4 3 4 4 4 4.0 4.0 4 4 3 3.9
I am using the plotrix and devtools packages, and have already installed them both, and the barp2 function like this:
# install the packages and load the barp2 function
install.packages('plotrix')
install.packages('devtools')
install_url("http://cran.r-project.org/src/contrib/Archive/plotrix/plotrix_3.5-2.tar.gz")
source_gist("https://gist.github.com/tleja/8592929")
# load the packages
library(plotrix)
library(devtools)
The modified code (barp2) that I'm using is available here.
I am trying to plot the data in the data frame, provided above, like this:
par(mar = c(5, 4, 4, 6))
barp2(df, pch=t(c(0:4, 7:14)), names.arg=rownames(df), legend.lab=colnames(df),
ylab="y label", main="main")
I am using the reference chart, to fill the bars of the plot.
I want the rownames of df to be the x axis labels, and the colnames of df to be in a legend.
However, I keep getting this error:
Error in axis(1, at = x, labels = names.arg, cex.axis = cex.axis) :
'at' and 'labels' lengths differ, 13 != 10
I understand that this is because rownames(df) has a length of 10 and colnames(df) has a length of 13 (which are clearly not equal), but I am not sure how to fix this problem, so that the data in the data frame is displayed in a barplot.
Or if I swap the columns and rows around using t(df), like this:
barp2(t(df), pch=t(c(0:4, 7:11)), names.arg=rownames(df), legend.lab=colnames(df),
ylab="y label", main="main")
I get this error:
Error in seq.default(x1[frect] + xinc[frect]/2, x2[frect] - xinc[frect]/2, :
wrong sign in 'by' argument
I have no idea what this error means or why I get it.
Sorry I can't provide an image of what it should look like, but hopefully you get the basic idea of it.
Any help would be much appreciated.
A bridge in a graph means if we remove it the graph will be disconnected !
so i want to know if there is way to find all bridges in a graph :
here is an example :
input
12 15
1 2
1 3
2 4
2 5
3 5
4 6
6 7
6 10
6 11
7 8
8 9
8 10
9 10
10 11
11 12
Output :
2 4
4 6
11 12
PLEASE DO NOT GIVE ME THE SOLUTION JUST A HINT !
Thanks
If you have the visiting number vn[v] and low number low[v] for each vertex v in graph G, then you can find if an edge is bridge of not (while unwinding the dfs recursive calls) using the following condition
if (low[w] > vn[v]) then (v,w) is a bridge