re-sequencing a column of numbers based on a set value, R - r

I have a column of numbers that need to be re sequenced starting from 1, for example:
column with numbers: 226 227 227 227 228 228 229 229 ...... so on
I would like to re sequence so that: 226 changes 1, 227 changes 2, 227 = 2, 227 = 2, 228 = 3, 228 = 3,
229 = 4, 229 = 4 ....... and so on
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 30%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<table>
<tr>
<th>Current</th>
<th>Desired</th>
</tr>
<tr>
<td>226</td>
<td>99</td>
</tr>
<tr>
<td>227</td>
<td>100</td>
</tr>
<tr>
<td>227</td>
<td>100</td>
</tr>
<tr>
<td>227</td>
<td>100</td>
</tr>
<tr>
<td>228</td>
<td>101</td>
</tr>
<tr>
<td>228</td>
<td>101</td>
</tr>
<tr>
<td>229</td>
<td>102</td>
</tr>
<tr>
<td>229</td>
<td>102</td>
</tr>
<tr>
<td>229</td>
<td>102</td>
</tr>
</table>
</body>
</html>

You can try as.numeric + factor like below, i.e.,
r <- as.numeric(factor(v))
such that
> r
[1] 1 2 2 2 3 3 4 4
DATA
v <- c(226, 227, 227, 227, 228, 228, 229, 229)

We can subtract the minimum or the first element of 'Current' and then add 99 to it
df1$Desired <- df1$Current - df1$Current[1] + 99
df1$Desired
#[1] 99 100 100 100 101 101 102 102 102
Or if it needs to be resequenced from 1
df1$Current - df1$Current[1] + 1
Or use match
match(df1$Current, unique(df1$Current))
data
df1 <- data.frame(Current = c(226, 227, 227, 227, 228, 228, 229, 229, 229))

Related

bar charts in ggplot2 with .csv files

I think my question is very basic, but I have just begun to learn R. Today I have spent a long time trying to import data from .csv files. I have 25 .csv files, which are all structured like the example file. From the data I would like to create graphs like I did with Excel 1, because with ggplot2 I can create much nicer graphs than with Excel.
In my csv files I have data from different organisms, the concentration of formed compounds (molecule1 and molecule2) and the standard deviation (molecule1sd, molecule2sd). In my figures I want to show the data for one compound with standard deviations for all organisms as a bar chart, which is overlaid with a line chart with the concentration without organisms.
However, somehow I can' t get this to work. I have tested this, but somehow it doesn' t work.
I need your help
+ usePackage("data.table")
> usePackage("ggplot2")
>
> # load packages
> library(data.table)
> library(ggplot2)
>
> results <- fread("Results.csv", header=TRUE, sep=";")
>
> str(results)
Classes ‘data.table’ and 'data.frame': 7 obs. of 5 variables:
$ Organism : chr "AC1432" "D3425" "BF3523" "XR2405" ...
$ Molecule1 : num 39.5 418.4 189.2 49.3 4610.9 ...
$ Molecule1sd: num 19.6 70.9 102.8 21.2 275.9 ...
$ Molecule2 : num 276 6511 235 500 11205 ...
$ Molecule2sd: num 21 291.1 109.7 67.1 94.5 ...
- attr(*, ".internal.selfref")=<externalptr>
>
> df <- data.frame(results)
>
> str(df)
'data.frame': 7 obs. of 5 variables:
$ Organism : chr "AC1432" "D3425" "BF3523" "XR2405" ...
$ Molecule1 : num 39.5 418.4 189.2 49.3 4610.9 ...
$ Molecule1sd: num 19.6 70.9 102.8 21.2 275.9 ...
$ Molecule2 : num 276 6511 235 500 11205 ...
$ Molecule2sd: num 21 291.1 109.7 67.1 94.5 ...
>
> ggplot(data=df, aes(x=Organism)+
+ geom_bar(aes(fill=Organism)) + geom_errorbar(data=df, mapping=aes(x="Organism", ymin="Molecule1sd", ymax="Molecule1sd"), width=0.2, size=1, color="blue")
+
Data from results.csv in the following table:
</head>
<body link="#0563C1" vlink="#954F72">
<table border=0 cellpadding=0 cellspacing=0 width=480 style='border-collapse:
collapse;table-layout:fixed;width:360pt'>
<col width=80 span=6 style='width:60pt'>
<tr height=20 style='height:15.0pt'>
<td height=20 width=80 style='height:15.0pt;width:60pt'>Organism</td>
<td width=80 style='width:60pt'>Molecule1</td>
<td width=80 style='width:60pt'>Molecule1sd</td>
<td width=80 style='width:60pt'>Molecule2</td>
<td colspan=2 width=160 style='mso-ignore:colspan;width:120pt'>Molecule2sd</td>
</tr>
<tr height=20 style='height:15.0pt'>
<td height=20 style='height:15.0pt'>AC1432</td>
<td class=xl65 align=right>39.5</td>
<td class=xl65 align=right>19.6</td>
<td class=xl65 align=right>275.9</td>
<td class=xl65 align=right>21.0</td>
<td></td>
</tr>
<tr height=20 style='height:15.0pt'>
<td height=20 style='height:15.0pt'>D3425</td>
<td class=xl65 align=right>418.4</td>
<td class=xl65 align=right>70.9</td>
<td class=xl65 align=right>6511.0</td>
<td class=xl65 align=right>291.1</td>
<td></td>
</tr>
<tr height=20 style='height:15.0pt'>
<td height=20 style='height:15.0pt'>BF3523</td>
<td class=xl65 align=right>189.2</td>
<td class=xl65 align=right>102.8</td>
<td class=xl65 align=right>235.2</td>
<td class=xl65 align=right>109.7</td>
<td></td>
</tr>
<tr height=20 style='height:15.0pt'>
<td height=20 style='height:15.0pt'>XR2405</td>
<td class=xl65 align=right>49.3</td>
<td class=xl65 align=right>21.2</td>
<td class=xl65 align=right>499.9</td>
<td class=xl65 align=right>67.1</td>
<td></td>
</tr>
<tr height=20 style='height:15.0pt'>
<td height=20 style='height:15.0pt'>XR2463</td>
<td class=xl65 align=right>4610.9</td>
<td class=xl65 align=right>275.9</td>
<td class=xl65 align=right>11205.3</td>
<td class=xl65 align=right>94.5</td>
<td></td>
</tr>
<tr height=20 style='height:15.0pt'>
<td height=20 style='height:15.0pt'>ATF259</td>
<td class=xl65 align=right>751.7</td>
<td class=xl65 align=right>71.6</td>
<td class=xl65 align=right>9507.9</td>
<td class=xl65 align=right>707.2</td>
<td></td>
</tr>
<tr height=20 style='height:15.0pt'>
<td height=20 style='height:15.0pt'>without organism</td>
<td align=right>35</td>
<td></td>
<td align=right>250</td>
<td colspan=2 style='mso-ignore:colspan'></td>
</tr>
<![if supportMisalignedColumns]>
<tr height=0 style='display:none'>
<td width=80 style='width:60pt'></td>
<td width=80 style='width:60pt'></td>
<td width=80 style='width:60pt'></td>
<td width=80 style='width:60pt'></td>
<td width=80 style='width:60pt'></td>
<td width=80 style='width:60pt'></td>
</tr>
<![endif]>
</table>
Thank you for your help
Try this as a starting point:
ggplot( df ,aes( Organism, Molecule1, fill=Organism )) +
geom_bar( stat="identity" ) +
geom_errorbar( aes(ymin=Molecule1-Molecule1sd, ymax=Molecule1+Molecule1sd), width=.2 )
Changing the details can be done later easily, once you have a working backbone.
Saving can be done outside of ggplot, simply by calling:
png("yourfile.png")
<plotting stuff>
dev.off()
Result:

Sum of groupBy elements using PipeTransform

I did a groupBy in an Angular project and in back-end, using ASP.NET Web Api to retrieve data for the groupBy functionality. I used this link to make it work - Angular groupBy that uses PipeTransform interface for the implementation. It works fine and the current output is as follows right now:
Jack
ID - Date - Points
1001 - 04/01/2020 - 10
1001 - 05/01/2020 - 10
John
ID - Date - Points
1002 - 04/01/2020 - 10
1002 - 05/01/2020 - 20
So the following is the data structure:
data = [
{ "id": "1001", "name": "Jack", "date": "2020-01 - 05T00: 00: 00", "points": 10 },
{ "id": "1001", "name": "Jack", "date": "2020-01 - 04T00: 00: 00", "points": 10 },
{ "id": "1002", "name": "John", "date": "2020-01 - 05T00: 00: 00", "points": 20 },
{ "id": "1002", "name": "John", "date": "2020-01 - 04T00: 00: 00", "points": 10 }
];
In the front-end, I did this to render the view:
<div class="col-xs-12 table-responsive">
<table *ngIf="data" class="table table-responsive" border="0">
<tbody *ngFor="let details of data | groupBy: 'name'">
<tr>
<th colspan="2">
<h2>{{details.key}}</h2> <!--Here name is shown-->
</th>
</tr>
<tr>
<th>Sl No.</th>
<th>Id</th>
<th>Name</th>
<th>Date</th>
<th>Points</th>
</tr>
<tr *ngFor="let value of details.value; let i = index">
<td>{{i + 1}}</td>
<td>{{value.id}}</td>
<td>{{value.name}}</td>
<td>{{value.date | date :'dd-MMM-yyyy'}}</td>
<td>{{value.points}}</td>
</tr>
<tr>Total Points: {{sum}}</tr> <!--Expecting total points for groupBy-->
</tbody>
</table>
</div>
My expected output is as follows:
Jack
ID - Date - Points
1001 - 04/01/2020 - 10
1001 - 05/01/2020 - 10
______________________________
Total Points: 20
John
ID - Date - Points
1002 - 04/01/2020 - 10
1002 - 05/01/2020 - 20
______________________________
Total Points: 30
I am new to stackblitz and unable to build the project to render. Here is the link that I tried with the code - Angular groupBy with stackblitz
N.B: I tried to do the sum in the TypeScript file like this but this does total calculation of the points, not groupBy:
public sum: number = 0;
GetUserPoints() {
debugger;
for (let i = 0; i <= this.data.length; i++) {
this.sum += this.data[i].points;
console.log(this.data.length);
}
}
if you want to keep this dynamic and reusable by using pipes, you could achieve calculating the sum of points by creating a PluckPipe and SumPipe like i did in this stackblitz
Then just use <tr>Total Points: {{details.value | pluck:'points' | sum}}</tr> in your template
example: https://stackblitz.com/edit/angular-iurvtc
btw: your stackblitz did not render because there were spaces in the timestamps.

how can I put my canvas next to my table automatically if the screen is wide enough and else underneath each other?

I have a table with data and a graph (chart.js) visualizing this data.
The graph is in a "canvas"-type.
I can't get them to be next to each other.
I tried:
float:left
putting them both in the same div
column-count
... all giving wrong results.
Float:left puts only a bit of the graph next to the table, in the same div gives no change at all and column-count behaves somewhat like float-left.
<script src="/Chart.js"></script>
<article>
<h2>my data</h2>
<table><caption>nice data</caption><thead>
<tr><th>some value</th><th>%</th><th>more values</th></tr></thead>
<tbody>
<tr><td>€ 100,00</td><td>17,59%</td><td>10 0 09 39 09 </td></tr>
<tr><td>€ 250,00</td><td>5,86%</td><td>0 79 7 35 5 </td></tr>
<tr><td>€ 10 000,00</td><td>5,48%</td><td>09 09 36 56 05 </td></tr>
<tr><td>€ 100 000,00</td><td>5,01%</td><td>79 35 85 94 03 </td></tr>
<tr><td>€ 107,50</td><td>4,11%</td><td>89 69 49 09 53 </td></tr>
<tr><td>€ 1 000,00</td><td>3,06%</td><td>28 06 15 51 20 </td></tr>
<tr><td>€ 2 500,00</td><td>2,79%</td><td>10 39 77 17 25 </td></tr>
</tbody></table>
<div style="height:80vh;"><canvas id="bedrag_perc"></canvas></div>
<script>
var ctx = document.getElementById("bedrag_perc");
var myChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: [100, 107, 250, 1000, 10000, 100000, 3.14],
datasets: [{
label: 'so much data',
data: ['17.59', '4.11', '5.86', '3.06', '5.48', '5.01', '41.11'],
}]
},
options: {
animation: { duration: 0 },
responsive: true,
maintainAspectRatio: false
}
});
</script>
</article>
I would like to have the graph next to the table if the screen is wide enough, else underneath each other (responsive web design).
The actual result is that only half of the chart is next to the table.
i have used bootstrap to make it work. below is the link. hope it helps.
http://jsfiddle.net/3vxtw786/1/999

Deduction List - ASP Arrays

I want to display a breakdown of total investment deductions as an array list. The total amount of deductions is $-20.24 and my list breakdown is not adding up to that amount. I'm not too sure where I got it wrong. Please review my code and provide feedback. See total value being returned below:
Date Units Unit price Value
30/04/2018 -4.203 $ 1.99143 $ -8.37
30/04/2018 -0.366 $ 1.99454 $ -0.73
30/04/2018 -1.576 $ 3.54061 $ -5.58
30/04/2018 -0.138 $ 3.55072 $ -0.49
30/04/2018 -1.871 $ 2.49065 $ -4.66
30/04/2018 -0.164 $ 2.50000 $ -0.41
Total amount $ 16.98
<%
Dim objMemberClient, SwitchList
set objMemberClient = Server.createObject("MemberServiceProxy")
SwitchList= objMemberClient.GetInvestmentTransactionObjList(session("MemberId"),session("FundCode"), request.Querystring("date"), request.Querystring("date"), request.Querystring("description"))
%>
<h1>Investments</h1>
<div class="table-responsive">
<%if request.Querystring("description") = "Deduction" then %>
<TABLE class="table">
<%for i = LBound(SwitchList) to UBound(SwitchList)%>
<%if SwitchList(i).DeductionCode = getDesc(request.Querystring("subtype")) then%>
<tr>
<%if SwitchList(i).DeductionSign = true then%>
<td class="table_Header" width="200px">Investment sold</td>
<%exit for%>
<%end if%>
<%end if%>
<%Next%>
<td class="table_Header" width="125px">Date</td>
<td class="table_Header" width="125px">Units</td>
<td class="table_Header" width="125px">Unit price</td>
<td class="table_Header" width="125px">Value</td>
</tr>
<%for i = LBound(SwitchList) to UBound(SwitchList)%>
<%if SwitchList(i).DeductionCode = getDesc(request.Querystring("subtype")) then%>
<tr>
<td valign="top" class="border_Bottom"> <%=SwitchList(i).InvestmentOption.Name%></td>
<td valign="top" class="border_Bottom"><%=SwitchList(i).InvestmentDate%></td>
<td valign="top" class="border_Bottom"> <%=SwitchList(i).NumberUnits%></td>
<%if SwitchList(i).DeductionSign = true then %>
<%total = total + SwitchList(i).SwitchOutDollarValue%>
<%total = total * -1%>
<td valign="top" class="border_Bottom">$ <%=FormatNumber(SwitchList(i).SwitchOutDollarValue/Replace(SwitchList(i).NumberUnits,"-",""),5)%></td>
<td valign="top" class="border_Bottom">$ -<%=FormatNumber(SwitchList(i).SwitchOutDollarValue,2)%></td>
<%end if%>
</TABLE>
It would be easier to debug if we had something we could execute, but just guessing I think your problem is here:
<%total = total + SwitchList(i).SwitchOutDollarValue%>
<%total = total * -1%>
Total is a negative value, SwitchOutDollarValue I believe is a positive value, so comibining them in this way is not going to get the result you want.
As #SearchAndResQ mentioned, I would remove <%total = total * -1%> from inside the for loop and move it to just before you display the total.

Customize Table in Bootstrap

I am creating an ecommerce website using the current Bootstrap version. On the admin panel I am trying to show all the product infos within a table in such a way that it looks like this :
+______+_____________+_____+_______+_____+
| name | description | qty | price | sku |
+______+_____________+_____+_______+_____+
| P1 | Description | 10 | 200 | 2ds |
+______+_____________+_____+_______+____ +
| Image 1 , Image 2 , Image 3, ...... |
+______+_____________+_____+_______+____ +
| P2 | Description | 14 | 500 | 3ds |
+______+_____________+_____+_______+____ +
| Image 1 , Image 2 , Image 3, ...... |
+______+_____________+_____+_______+____ +
| P3 | Description | 45 | 234 | 4xs |
+______+_____________+_____+_______+____ +
| Image 1 , Image 2 , Image 3, ...... |
+______+_____________+_____+_______+____ +
And so on. Hope I can make you understand what I am trying to do here. After every product row comes the product images.. then again another product row and its corresponding images and so on....
What I have tried :
<table class="table table-striped text-center">
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Description</th>
<th>Stock Qty</th>
<th>Shown Qty</th>
<th>Sale Price</th>
<th>Original Price</th>
<th>SKU</th>
<th>Image</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some Product</td>
<td>T Shirt</td>
<td>Description</td>
<td>24</td>
<td>500</td>
<td>BKSTU56V57</td>
</tr>
<tr><img src=".." /><img src="...."></tr>
</tbody>
</table>
And it's not working. taking the images to the top even before the <th>
You're missing the <td> element. Try this with column span:
<tr><td colspan="6"><img src=".." /><img src="...."></td></tr>

Resources