[R] YourCast Data Format

2011-01-08 Thread Thomas Jensen

Dear R-List,

I have data that is collected in panels like this (output from the  
dput() function, the first 20 observations in the data set):


structure(list(Country = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label =  
c(Belgium,

Denmark, Czech.Republic, Germany, Estonia, Greece,
Spain, France, Ireland, Italy, Cyprus, Latvia, Lithuania,
Luxembourg, Hungary, Malta, Netherlands, Austria, Poland,
Portugal, Slovenia, Slovakia, Bulgaria, Romania, Finland,
Sweden, UK), class = factor), Year = c(2003, 2003, 2003,
2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2004, 2004,
2004, 2004, 2004, 2004, 2004, 2004), Month = c(1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8), Yes = c(21L,
18L, 20L, 19L, 31L, 39L, 28L, 2L, 28L, 21L, 26L, 50L, 14L, 28L,
50L, 83L, 10L, 25L, 22L, 6L), No = c(0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 4L, 0L, 0L, 0L, 0L), Abstention =  
c(0L,

0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 3L, 0L,
0L, 0L, 0L), No.Neg = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Abstention.Neg = c(0L,
0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L,
0L, 0L, 0L), Yes.Neg = c(1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L,
0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L), Yes.Pos = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), Missing = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Enlargement = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1)), .Names =  
c(Country,

Year, Month, Yes, No, Abstention, No.Neg, Abstention.Neg,
Yes.Neg, Yes.Pos, Missing, Enlargement), row.names = c(NA,
20L), class = data.frame)

The data set has in total 27 countries for the years 1999 to 2008, but  
with unbalanced panels.


I want to be able to estimate a model and do forecasting for each  
country in the data set. I have been looking into the YourCast package  
from King et al. but since I have all my data in a single file, I am  
at a loss as to how to create a data object that the yourcast()  
function will accept. Does anyone know how to do this without going  
through the tedious procedure of manually splitting the data file up  
into the different cross sections?


Best, Thomas

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Voting power indices

2010-09-17 Thread Thomas Jensen

Dear R-experts,

I was wondering if anyone know of a library in R that has functions  
for calculating the Banzhaf and Shaply-Shubik indeces?


Best, Thomas

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Reshaping data

2010-07-20 Thread Thomas Jensen
Hi Dennis,

Thanks for the answer, it works perfectly for two time intervals, but  
if I add a third interval like this:

ID  begin_t1end_t1  begin_t2end_t2  
begin_t3end_t3
Thomas  11/03/0413/05/0604/02/0716/05/08
09/06/0927/01/10
... ... ... ... 
...
Jens24/01/0223/05/0307/06/0314/11/05
16/02/0624/08/09

and write out the reshape function like this:

d2 - reshape(d, varying = list(c(begin_t1, begin_t2, begin_t3),  
c(end_t1, end_t2, end_t3)), v.names = c('Begin_Time',  
'End_Time'), idvar = 'ID', direction = 'long')

then I get the following error:

Error in `[.data.frame`(data, , varying[[j]][i]) :
   undefined columns selected

What am i doing wrong?

Best, Thomas

On 20 Jul 2010, at 04:44, Dennis Murphy wrote:

 Hi:

 Here's one solution using function reshape() in the stats package  
 (adapted from an R-help solution by Thomas Lumley on Nov. 26, 2002):


 d - read.table(textConnection(
 + ID  begin_t1end_t1   
 begin_t2end_t2
 + Thomas  11/03/0413/05/0604/02/07 
 16/05/08
 + Jens24/01/0223/05/0307/06/03 
 14/11/05),
 + header = TRUE)
  d
   ID begin_t1   end_t1 begin_t2   end_t2
 1 Thomas 11/03/04 13/05/06 04/02/07 16/05/08
 2   Jens 24/01/02 23/05/03 07/06/03 14/11/05

 d2 - reshape(d, varying = list(c('begin_t1', 'begin_t2'),  
 c('end_t1', 'end_t2')),
   v.names = c('Begin_Time', 'End_Time'), idvar = 'ID',  
 direction = 'long')

 # Result could use some cleanup:
 rownames(d2) - NULL# clear the created row names
 d2 - d2[order(d2$ID), -2]  # sort by name, removing the  
 time order (variable 2)
 d2
   ID Begin_Time End_Time
 2   Jens   24/01/02 23/05/03
 4   Jens   07/06/03 14/11/05
 1 Thomas   11/03/04 13/05/06
 3 Thomas   04/02/07 16/05/08

 HTH,
 Dennis

 On Mon, Jul 19, 2010 at 3:48 PM, Thomas Jensen 
 thomas.jen...@eup.gess.ethz.ch 
  wrote:
 Dear All,

 I have some data in the following shape:

 ID  begin_t1end_t1   
 begin_t2end_t2
 Thomas  11/03/0413/05/0604/02/07 
 16/05/08
 ... ... ... ...   
   ...
 Jens24/01/0223/05/0307/06/03 
 14/11/05

 I would like to reshape this data to have the following form:

 ID  Begin_Time  End_Time
 Thomas  11/03/0413/05/06
 Thomas  04/02/0716/05/08
 ... ... ...
 Jens24/01/0223/05/03
 Jens07/06/0314/11/05

 I have been doing some google searches and looked at the reshape  
 library, but so far I have not been able to shape the data like I  
 want. If you guys could help, I would greatly appreciate it!

 Best, Thomas

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.



[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Reshaping data

2010-07-19 Thread Thomas Jensen

Dear All,

I have some data in the following shape:

ID  begin_t1end_t1  begin_t2end_t2
Thomas  11/03/0413/05/0604/02/0716/05/08
... ... ... ... 
...
Jens24/01/0223/05/0307/06/0314/11/05

I would like to reshape this data to have the following form:

ID  Begin_Time  End_Time
Thomas  11/03/0413/05/06
Thomas  04/02/0716/05/08
... ... ...
Jens24/01/0223/05/03
Jens07/06/0314/11/05

I have been doing some google searches and looked at the reshape  
library, but so far I have not been able to shape the data like I  
want. If you guys could help, I would greatly appreciate it!


Best, Thomas

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Help with dates and characters

2010-06-29 Thread Thomas Jensen

Dear R Experts,

I have a vector of dates in character format like this:

date
2000-01-01
2000-01-23
2001-03-12
...
...
...
2009-12-31

I would like to delete the last part of the character string (i.e. the  
day part), so the vector looks like this:


date
2000-01
2000-01
2001-03
...
...
...
2009-03

I have been looking into regular expressions, but i find this very  
confusing.


Thank you for your help,
Thomas

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Creating quarterly data

2010-06-28 Thread Thomas Jensen

Dear R Experts,

I have data in the following format

x1  x2  time
2   4   1
3   1   2
4   6   3
1   4   4   
5   6   5   
8   5   6
.   .   .
.   .   .
.   .   .
1   5   399
3   4   400

Time represents each month which simply has a number from 1 to 400  
(i.e. the data covers 400 months). I would like to somehow create two  
new variables which counts x1 and x2 as quarterly data in stead of  
monthly. Like this:


x1  x1  Quarter
10  12  1
12  15  2
.   .   .
.   .   .
.   .   .
23  21  100

Can any one tell me how this can be done in R?

With kind regards,
Thomas Jensen

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Creating quarterly data

2010-06-28 Thread Thomas Jensen

Thanks, Allan, that did the trick :)

Best, Thomas
On Jun 28, 2010, at 6:13 PM, Allan Engelhardt wrote:


One approach:

d - data.frame(x1=c(2,3,4,1,5,8), x2=c(4,1,6,4,6,5), time=1:6)
d$quarter - (d$time-1) %/% 4  #  Or whatever your logic is
aggregate(cbind(x1,x2) ~ quarter, data = d, sum)
#   quarter x1 x2
# 1   0 10 15
# 2   1 13 11


Hope this helps

Allan


On 28/06/10 13:23, Thomas Jensen wrote:

Dear R Experts,

I have data in the following format

x1x2time
241
312
463
144
565
856
...
...
...
15399
34400

Time represents each month which simply has a number from 1 to 400  
(i.e. the data covers 400 months). I would like to somehow create  
two new variables which counts x1 and x2 as quarterly data in stead  
of monthly. Like this:


x1x1Quarter
10121
12152
...
...
...
2321100

Can any one tell me how this can be done in R?

With kind regards,
Thomas Jensen

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Event History Data Recoding

2010-04-23 Thread Thomas Jensen
Dear R list,

I have an event history data set that is structured like this:

Legislative act Discussion Agreement Time Event
Act12006-05-30 2006-06-19 201
Act22004-03-01 2004-06-14 105   1
.   
.
.

I have information on the meetings in the legislature between adoption
periods in a separate variable (the start and stop dates are included):

Act1 meeting:2006-05-30, 2006-06-19.
Act2 meeting: 2004-03-22, 2004-04-26, 2004-06-14.

I want to code this as a discrete event data set, so it should look like
this:

Legislative act Event Meeting Time Count
Act1  0   2006-05-30  1
Act1  1   2006-06-07  2
Act2  0   2004-03-22  1
Act2  0   2004-04-26  2
Act2  1   2004-06-14  3

Can anyone tell me if it is possible to do this recoding in R, or do I
have to do it by hand?

Best, Thomas

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Event History Data Recoding

2010-04-23 Thread Thomas Jensen
Thanks Josh,

But I am not sure if the reshape function can create new rows based on
the meeting variable. For the second act there are three meetings, i.e.
one meeting between discussion and agreement, and this should be entered
as a separate row.

Best, Thomas

On Fri, 2010-04-23 at 07:45 -0700, Joshua Wiley wrote:
 If you are talking about changing from a wide format to long take a look 
 at
 
 ?reshape
 
 
 ---
 Josh
 
 On Fri, Apr 23, 2010 at 7:09 AM, Thomas Jensen
 thomas.jen...@eup.gess.ethz.ch wrote:
  Dear R list,
 
  I have an event history data set that is structured like this:
 
  Legislative act Discussion Agreement Time Event
  Act12006-05-30 2006-06-19 201
  Act22004-03-01 2004-06-14 105   1
  .
  .
  .
 
  I have information on the meetings in the legislature between adoption
  periods in a separate variable (the start and stop dates are included):
 
  Act1 meeting:2006-05-30, 2006-06-19.
  Act2 meeting: 2004-03-22, 2004-04-26, 2004-06-14.
 
  I want to code this as a discrete event data set, so it should look like
  this:
 
  Legislative act Event Meeting Time Count
  Act1  0   2006-05-30  1
  Act1  1   2006-06-07  2
  Act2  0   2004-03-22  1
  Act2  0   2004-04-26  2
  Act2  1   2004-06-14  3
 
  Can anyone tell me if it is possible to do this recoding in R, or do I
  have to do it by hand?
 
  Best, Thomas
 
  __
  R-help@r-project.org mailing list
  https://stat.ethz.ch/mailman/listinfo/r-help
  PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
  and provide commented, minimal, self-contained, reproducible code.
 
 
 
 

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Create a new variable

2010-03-30 Thread Thomas Jensen
Dear R-list,

Sorry for spamming the list lately, I am just learning the more advanced
aspects of R! 

I have some data that looks like this:

Out Country1 Country 2 Country 3 ... CountryN
1   1   1   1   1
0   1   1   0   1
1   1   0   1   0

I want to create a new variable that counts the number of zeros in every
row whenever Out is equal to 1, and else it is a zero, so it would look
like this:

new_var
0 
0
2

I have tried the following:

for (i in length(Out)){
if (Out == 1) {new_var - sum(dat[i,] != 1)}
else {new_var - 0}
}

but this gives me an error message.

Best, Thomas

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Finding common an unique elements in character vectors

2010-03-29 Thread Thomas Jensen
Dear R-list,

I have a problem which I think is quite basic, but so far google has not
helped me.

I have two vectors like this:

vector_1 - c(Belgium, Spain, Greece, Ireland, Luxembourg, Netherlands,
Portugal)

vector_2 - c(Denmark, Luxembourg)

I would like to find the elements in vector_1 that are not in vector_2

so that i get a vector with these countries: Belgium, Spain, Greece,
Ireland, Netherlands, Portugal.

Thanks a lot,

Thomas

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Transform data set

2010-03-23 Thread Thomas Jensen
Dear R Experts,

I am having some trouble creating a variable in R. I have data on
self-placement of voters, their placement of parties, and which party
they feel closest to. The data is structured like this:

 Party_Closelrplaceself lrplaceParty1   lrplaceParty2 ...   
   party1   2   4   5 
   party2   5   6   4
   party1   6   2   1

etc...

I want to format the data set so it looks like this:

Party_Close lrplacepartyclose   lrplaceself
  party14   2
  party24   5
  party12   6

Any help is greatly appreciated!

With kind regards,
Thomas

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Count of entries in a row of a data frame

2010-03-18 Thread Thomas Jensen
Dear R-list,


I have data in the following format:

Country 1   Country 2Country 3 ...
1   0   10  
2   1   02
3   0   10

Now I would like to create a variable that counts every non-zero element
of the rows, so for the above example it would be:

Count
1   1
2   2
3   1

Any help from is greatly appreciated!

With kind regards
Thomas

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Breadt-first-search algorithm

2010-03-05 Thread Thomas Jensen

Dear R-list,

does anyone of you know whether there exist a breadth-first-search  
algorithm for R?


Best, Thomas Jensen

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] selecting columns based on values of two variables

2009-09-06 Thread Thomas Jensen
Dear R-list,

I am having troubles selecting rows from a very large data-set  
containing distances between capitals.

The structure of the data-set looks like this:

numaida numbidb kmdist  midist
12  USA 20  CAN 731 456
22  USA 31  BHM 16231012
32  USA 40  CUB 18131130


I want to select a subset of these dyads, and have tried the following  
code:

subset(capdist,ida == c(DEN,SWD,FIN)  idb ==  
c(DEN,SWD,FIN))

This should ideally give me the dyads involving only Denmark, Sweden  
and Finland, however i get the error message:

[1] numa   idanumb   idbkmdist midist
0 rows (or 0-length row.names)
Warning messages:
1: In is.na(e1) | is.na(e2) :
   longer object length is not a multiple of shorter object length
2: In `==.default`(ida, c(DEN, SWD, FIN)) :
   longer object length is not a multiple of shorter object length
3: In is.na(e1) | is.na(e2) :
   longer object length is not a multiple of shorter object length
4: In `==.default`(idb, c(DEN, SWD, FIN)) :
   longer object length is not a multiple of shorter object length

Any help would be greatly appreciated,

Best, Thomas Jensen
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Creating adjacency matrix from a data frame

2009-09-06 Thread Thomas Jensen

Dear R-list,

Sorry for spamming the list, but I am just learning how to manipulate  
data in R, so  if this is a trivial question I am sorry.


I have the following data which list the distance between capitals:

ida idb kmdist
7108UK  BEL  313
7110UK  FRN  365
7116UK  POR 1618

I would like to convert this into an a valued adjacency matrix that  
looks like this:


BEL FRN POR
UK  313 365 1618

The full data set has all possible pairings between countries, so the  
end goal should be a symmetric adjacency matrix.


Thank you,

Thomas Jensen

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.