I have a data base with 121 rows and like 10 columns. One of these columns corresponds to Station, another to depth and the rest to chemical variables (temperature, salinity, etc.). I want to calculate the integrated value of these chemical properties by station, using the function oce::integrateTrapezoid. It's my first time doing a loop, so i dont know how. Could you help me?
dA<-matrix(data=NA, nrow=121, ncol=3)
for (Station in unique(datos$Station))
{dA[Station, cd] <- integrateTrapezoid(cd, Profundidad..m., "cA")
}
Station
Depth
temp
1
10
28
1
50
25
1
100
15
1
150
10
2
9
27
2
45
24
2
98
14
2
152
11
3
11
28.7
3
48
23
3
102
14
3
148
9
I have the following dataframe (em) (excerpt):
Year emissions Poll Country Sector
1993 0.00000 CO2 Austria 6
2006 0.00000 CO2 Austria 6.3
2015 0.00000 CO2 Austria 6
1998 12.07760 CO2 Austria 5.1
1992 11.12720 CO2 Austria 5
1995 11.11040 CO2 Austria 5
2006 10.26000 CO2 Austria 5
1998 0.00000 CO2 Austria 6.4.a
Then I have another dataframe (UN_ETScat). This dataframe contains certain, but not all, sector factors which are also contained in the em dataframe. I would like to get the sum of column emissions in dataframe em given that the sector (labeled UN_Cat in second dataframe) is contained in UN_ETScat.
UN_Cat Represented_in_ETS_Cat
1 1.A.1.a 20
2 1.A.1.b 20
3 1.A.2.a 20
4 1.A.2.b 20
5 1.A.2.c 20
6 1.A.2.d 20
7 1.A.2.e 20
8 1.A.2.f 20
9 1.A.2.g 20
10 1.A.3.e 20
11 1.A.4.a 20
12 1.A.4.c 20
13 1.B 20
14 1.A.1.b 21
15 1.A.1.c 21
16 1.A.2.c 21
17 1.B.2.c 21
For this purpose I have created variable x which represents column UN_Cat from the dataframe above.
as a layperson, I decided to code like this:
sum(em$emissions[UN_ETScat$x])
However, I only get [1] 0 which is not true. What did I do wrong? Thanks for any help and suggestions.
Nordsee
If I get your question right:
sum(em$emissions[ em$sector %in% unique(UN_ETScat$UN_Cat) ])
might solve your problem.
This question already has answers here:
Create a sequential number (counter) for rows within each group of a dataframe [duplicate]
(6 answers)
Closed 5 years ago.
First time posting, mainly because I got tired of banging my head against the wall.
Thanks in advance for looking at this.
I have a data frame that looks like this:
state city x y z
1 OR Portland 8 10 1
2 OR Portland 8 10 4
3 OR Portland 8 10 10
4 NY New York 29 15 10
5 NY New York 29 15 18
6 NJ Trenton 8 10 50
7 NJ Trenton 8 10 60
8 NJ Trenton 8 10 70
9 WA Seattle 1 70 6
10 WA Seattle 1 70 7
11 WA Seattle 1 70 8
12 WA Seattle 1 70 9
13 WA Seattle 1 70 10
14 WA Seattle 1 70 11
I have been trying to reshape it to look like this:
state city x y z.1 z.2 z.3 z.4 z.5 z.6
OR Portland 8 10 1 4 10
NY New York 29 15 10 18
NJ Trenton 8 10 50 60 70
WA Seattle 1 70 6 7 8 9 10 11
I have been using the package reshape2 and the code looks like this:
df <- melt(data,id.vars = c("state","city","x","y"),measure.vars = "z")
wide <- dcast(df, state + city + x + y ~ variable)
Which returns a count of variable z for each set of id.vars.
I also tried this:
wide <- dcast(df, state + city + x + y ~ value)
Which looks like this:
state city x y 1 4 6 7 etc...
OR Portland 8 10 1 1 0 0
NY New York 29 15 0 0 0 0
NJ Trenton 8 10 0 0 0 0
WA Seattle 1 70 0 0 1 1
This is closer to what I'm looking for but would be very difficult to use for looking up information.
Tell me if I'm wrong, but it looks like I need an id variable for each duplicate value of state, city, x, y.
I haven't been able to think up or find anything that will allow me to create column that will number duplicate values like below.
state city x y z num
1 OR Portland 8 10 1 1
2 OR Portland 8 10 4 2
3 OR Portland 8 10 10 3
4 NY New York 29 15 10 1
5 NY New York 29 15 18 2
6 NJ Trenton 8 10 50 1
7 NJ Trenton 8 10 60 2
8 NJ Trenton 8 10 70 3
9 WA Seattle 1 70 6 1
10 WA Seattle 1 70 7 2
11 WA Seattle 1 70 8 3
12 WA Seattle 1 70 9 4
13 WA Seattle 1 70 10 5
14 WA Seattle 1 70 11 6
I would appreciate any help or an idea of where to keep looking for solutions.
Best,
-n
If using dplyr is an option you can use:
library(dplyr)
df %>%
group_by(state,city, x, y) %>%
mutate(n = row_number()) %>%
spread(n, z, sep = '')
Note that the ordering is lost tho
NOTE: This is a modified version of How do I turn monadic data into dyadic data in R (country-year into pair-year)?
I have data organized by country-year, with a ID for a dyadic relationship. I want to organize this by dyad-year.
Here is how my data is organized:
dyadic_id country_codes year
1 1 200 1990
2 1 20 1990
3 1 200 1991
4 1 20 1991
5 1 200 1991
6 1 300 1991
7 1 300 1991
8 1 20 1991
9 2 300 1990
10 2 10 1990
11 3 100 1990
12 3 10 1990
13 4 500 1991
14 4 200 1991
Here is how I want the data to be:
dyadic_id_want country_codes_1 country_codes_2 year_want
1 1 200 20 1990
2 1 200 20 1991
3 1 200 300 1991
4 1 300 20 1991
5 2 300 10 1990
6 3 100 10 1990
7 4 500 200 1991
Here is reproducible code:
dyadic_id<-c(1,1,1,1,1,1,1,1,2,2,3,3,4,4)
country_codes<-c(200,20,200,20,200,300,300,20,300,10,100,10,500,200)
year<-c(1990,1990,1991,1991,1991,1991,1991,1991,1990,1990,1990,1990,1991,1991)
mydf<-as.data.frame(cbind(dyadic_id,country_codes,year))
dyadic_id_want<-c(1,1,1,1,2,3,4)
country_codes_1<-c(200,200,200,300,300,100,500)
country_codes_2<-c(20,20,300,20,10,10,200)
year_want<-c(1990,1991,1991,1991,1990,1990,1991)
my_df_i_want<-as.data.frame(cbind(dyadic_id_want,country_codes_1,country_codes_2,year_want))
This is a unique problem since there are more than one country that participate in each event (noted by a dyadic_id).
You can actually do it very similar to akrun's solution for dplyr. Unfortunately I'm not well versed enough in data.table to help you with that part, and I'm sure others may have better solution to this one.
Basically for the mutate(ind=...) portion you need to be a little more clever on how you construct this indicator so that it is unique and will lead to the same result that you're looking for. For my solution, I notice that since you have groups of two, then your indicator should just have modulus operator attached to it.
ind=paste0('country_codes', ((row_number()+1) %% 2+1))
Then you need an indentifier for each group of two which again can be constructed using the similar idea.
ind_row = ceiling(row_number()/2)
Then you can proceed as normal in the code.
The full code is as follows:
mydf %>%
group_by(dyadic_id, year) %>%
mutate(ind=paste0('country_codes', ((row_number()+1) %% 2+1)),
ind_row = ceiling(row_number()/2)) %>%
spread(ind, country_codes) %>%
select(-ind_row)
# dyadic_id year country_codes1 country_codes2
#1 1 1990 200 20
#2 1 1991 200 20
#3 1 1991 200 300
#4 1 1991 300 20
#5 2 1990 300 10
#6 3 1990 100 10
#7 4 1991 500 200
All credit to akrun's solution though.
I have following table and data in MS.SQL Server
Id StudentName Roll SubjectName FM PM OM
1 Peter 1 English 100 40 80
2 Peter 1 Math 100 40 95
3 Peter 1 Science 100 40 75
4 Mike 2 English 100 40 75
5 Mike 2 Math 100 40 57
6 Mike 2 Science 100 40 48
7 Bella 3 English 100 40 85
8 Bella 3 Math 100 40 75
9 Bella 3 Science 100 40 65
How to print this data as continuous report using using ASP.net data report?
I used list control in report, but it is repeating all the rows as separate report. Here I want to print only 3 reports as per the Student.
We can groupwise the list control to solve the above problem.