Thomas Jensen
2009-Sep-06 13:54 UTC
[R] selecting columns based on values of two variables
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:
numa ida numb idb kmdist midist
1 2 USA 20 CAN 731 456
2 2 USA 31 BHM 1623 1012
3 2 USA 40 CUB 1813 1130
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 ida numb idb kmdist 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]]
Dimitris Rizopoulos
2009-Sep-06 14:59 UTC
[R] selecting columns based on values of two variables
probably you're looking for
subset(capdist, ida %in% c("DEN","SWD","FIN")
& idb %in%
c("DEN","SWD","FIN"))
I hope it helps.
Best,
Dimitris
Thomas Jensen wrote:> 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:
>
> numa ida numb idb kmdist midist
> 1 2 USA 20 CAN 731 456
> 2 2 USA 31 BHM 1623 1012
> 3 2 USA 40 CUB 1813 1130
>
>
> 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 ida numb idb kmdist 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 at 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.
>
--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
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
7108 "UK" "BEL" " 313"
7110 "UK" "FRN" " 365"
7116 "UK" "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