Plotting relatively large networks in R with igraph - r

I have two questions, the first is more complicated than the second.
I have a plot of my network below, but the plot is rather clustered how can I spread the nodes out more so I can clearly see the edges. I've tried a circular plot but I still got a rather clustered plot.
How can I rename the nodes? The current plot puts an 'X' in front of the node name, how can I remove the 'X'?
In my matrix an interacting node is marked with 1, non interacting nodes are marked with 0
Code:
library(igraph)
df <- as.matrix(read.table("sig_pairs_matrix_for_r.txt"))
g=graph.adjacency(df,mode="undirected",weighted=NULL,diag=FALSE)
plot.igraph(g,vertex.label=V(g)$name)
Matrix:
4 6 7 8 12 13 15 17 22 23 25 26 27 29 30 31 34 35 36 43 44 47 48 52 53 56 57 59 63 66 67 70 96 99 122 166 168 172 174 176 180 181 191 192 193 220 222 224 225 226 236 249 253 256 258 266 267 277 296
4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 1 1 0 1 1 1 1 1 0 1 1 1 0 0 0 0 0 1 0 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
8 0 0 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 0 1 0 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
12 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
13 0 0 0 1 0 0 0 0 0 0 0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
277 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 1 0 0 0
296 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0

For the first question, check ?igraph::layout. There's some instructions how you can alter the aligment of nodes.
For your second question, I guess the graph takes the names of the nodes from the row or column names of your matrix but read.table by default appends names with prefix X in order to make names starting with number valid variable names (see make.names). So rownames of your matrix are actually X1, X2 etc. You can circumvent this by reading your matrix like this:
df <- as.matrix(read.table("sig_pairs_matrix_for_r.txt",check.names=FALSE))
Or you could rename the rows and columns of your matrix before making the graph, i.e.
#newnames is a vector containing the new names, such as c("a","b",...)
rownames(df)<-colnames(df)<-newnames

For the second part of your question I would create a separate files for the row and column names. Once imported you can set the column and row names.
temp = read.csv("adj_matrix.csv", sep=",",head=FALSE)
adjmatrix <- as.matrix(temp)
temprow = read.csv("rownames.csv", sep=",",head=FALSE)
rowns <-as.matrix(temprow)
tempcol = read.csv("colnames.csv", sep=",",head=FALSE)
cols <-as.matrix(tempcol)
rownames(adjmatrix)<- rows
colnames(adjmatrix)<- cols
g1 <- graph.adjacency(adjmatrix, mode=c("directed"), weighted=NULL, diag=TRUE)
plot.igraph(g1)

Related

Converting a single cell count matrix that is currently a text file into a data frame

I have downloaded some files off the internet that are single cell count matrices, and I want to convert them into dataframes in R. I have used readLines to read the text files in, but now each line of the file looks like this:
3110082J24Rik\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0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t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0
For some context, the columns in a single cell matrix file are the cells, and the rows are the genes. The numbers in the file indicate the number of times that the gene (for this line, 3110082J24Rik) appears in each cell. Anyone have any idea how I could do this? Here is some sample data:
ID W308561 W308562 W308563 W308564 W308565 W308566 W308567 W308568 W308569 W308570 W308571 W308572 W308573 W308574 W308575 W308576 W308577 W308578 W308579 W308580 W308581 W308582 W308583 W308584 W308585 W308586 W308587 W308588 W308589 W308590 W308591 W308592 W308593 W308594 W308595 W308596 W308597 W308598 W308599 W308600 W308601 W308602 W308603 W308604 W308605 W308606 W308607 W308608 W308609 W308610 W308611 W308612 W308613 W308614 W308615 W308616 W308617 W308618 W308619 W308620 W308621 W308622 W308623 W308624 W308625 W308626 W308627 W308628 W308629 W308630 W308631 W308632 W308633 W308634 W308635 W308636 W308637 W308638 W308639 W308640 W308641 W308642 W308643 W308644 W308645 W308646 W308647 W308648 W308649 W308650 W308651 W308652 W308653 W308654 W308655 W308656 W308657 W308658 W308659 W308660 W308661 W308662 W308663 W308664 W308665 W308666 W308667 W308668 W308669 W308670 W308671 W308672 W308673 W308674 W308675 W308676 W308677 W308678 W308679 W308680 W308681 W308682 W308683 W308684 W308685 W308686 W308687 W308688 W308689 W308690 W308691 W308692 W308693 W308694 W308695 W308696 W308697 W308698 W308699 W308700 W308701 W308702 W308703 W308704 W308705 W308706 W308707 W308708 W308709 W308710 W308711 W308712 W308713 W308714 W308715 W308716 W308717 W308718 W308719 W308720 W308721 W308722 W308723 W308724 W308725 W308726 W308727 W308728 W308729 W308730 W308731 W308732 W308733 W308734 W308735 W308736 W308737 W308738 W308739 W308740 W308741 W308742 W308743 W308744 W308745 W308746 W308747 W308748 W308749 W308750 W308751 W308752 W308753 W308754 W308755 W308756 W308757 W308758 W308759 W308760 W308761 W308762 W308763 W308764 W308765 W308766 W308767 W308768 W308769 W308770 W308771 W308772 W308773 W308774 W308775 W308776 W308777 W308778 W308779 W308780 W308781 W308782 W308783 W308784 W308785 W308786 W308787 W308788 W308789 W308790 W308791 W308792 W308793 W308794 W308795 W308796 W308797 W308798 W308799 W308800 W308801 W308802 W308803 W308804 W308805 W308806 W308807 W308808 W308809 W308810 W308811 W308812 W308813 W308814 W308815 W308816 W308817 W308818 W308819 W308820 W308821 W308822 W308823 W308824 W308825 W308826 W308827 W308828 W308829 W308830 W308831 W308832 W308833 W308834 W308835 W308836 W308837 W308838 W308839 W308840 W308841 W308842 W308843 W308844 W308845 W308846 W308847 W308848 W308849 W308850 W308851 W308852 W308853 W308854 W308855 W308856 W308857 W308858 W308859 W308860 W308861 W308862 W308863 W308864 W308865 W308866 W308867 W308868 W308869 W308870 W308871 W308872 W308873 W308874 W308875 W308876 W308877 W308878 W308879 W308880 W308881 W308882 W308883 W308884 W308885 W308886 W308887 W308888 W308889 W308890 W308891 W308892 W308893 W308894 W308895 W308896 W308897 W308898 W308899 W308900 W308901 W308902 W308903 W308904 W308905 W308906 W308907 W308908 W308909 W308910 W308911 W308912 W308913 W308914 W308915 W308916 W308917 W308918 W308919 W308920 W308921 W308922 W308923 W308924 W308925 W308926 W308927 W308928 W308929 W308930 W308931 W308932 W308933 W308934 W308935 W308936 W308937 W308938 W308939 W308940 W308941 W308942 W308943 W308944
0610005C13Rik 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0610008F07Rik 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
In the ideal world, each of the labels at the top that starts with "W" would be a separate column with the corresponding numbers for each gene also in that column.

Error with foodweb package

I am using foodweb package to analyse a bunch of food webs. I keep getting the following error when analyzing a particular food web.
Error in [<-.data.frame(tmp, by.level == 0, value = c(NA_real_, :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0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0
There is something wrong with the length, but I can´t figure it out. Any help with this? Thanks in advance!

Comparison of two dataframes

I have an excel table of 15200 lines, corresponding to a tree analyzed for its structures. I have all the structures in columns (48 structures), they have been counted on every tree. for example, the tree 12607 has 3 structures CV11, 1 structure IN12 and none (0) of the rest of all the structures. Thus, the table looks like a huge table with a lot of 0 and some numbers of the occurrence of structures on the trees. The very last column is the value given to the tree, according to the structures found on it (each structure giving a number of point to the tree by its presence on it).
The question is: Are there some structures, or combination of structures, which give a high value to the tree. Of course, according to the value of each structure, we can see which one has a higher value than the others (ex: structure CV11 has a value of 15, structure IN12 has a value of 4). But what I want to know is, if we take all the trees having a higher final value than 100 (we create a new dataframe "data100"), and we compare with the trees having a final value under 100 (we create another dataframe "data0"), can we find a significant difference in the number and occurrence of structures found on these trees? Because the structure with high value is maybe only found on the trees with a value under 100; because for example, this structure does not allow other structures to be found on the same tree.
Voilà, I hope I have given enough details... If you have any idea or proposition for solving this problem.. it will be great!
Below is my script.
> data100
CV11 CV12 CV13 CV14 CV15 CV21 CV22 CV23 CV24 CV25 CV26 CV31 CV32 CV33 CV41 CV42 CV43 CV44 CV51 CV52 IN11 IN12 IN13
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
6 0 0 0 0 0 0 0 0 0 0 0 0 0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
11 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 2 0 0 0 0 0
12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 3 0 0
13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 3 0 0
14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0
15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
EP11 EP12 EP13 EP14 EP21 EP31 EP32 EP33 EP34 EP35 NE11 NE12 NE21 OT11 OT12 OT21 OT22 ecoval
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 56
3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10
4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10
5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4
6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24
7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18
12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 63
13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 77
14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 54
15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20
[ reached getOption("max.print") -- omitted 60749 rows ]
> sortdata100<-data100[order(data100[,64],decreasing=T),]
> rsortdata100<-sortdata100[sortdata100$ecoval>100,]
> rsortdata100<-na.omit(rsortdata100)#181 lignes
> rsortdata100
CV11 CV12 CV13 CV14 CV15 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 0 0 0 326
2895 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 325
4719 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 324
682 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 311
3444 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 306
1299 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 302
2713 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 302
[ reached getOption("max.print") -- omitted 166 rows ]
> data0<-sortdata100[sortdata100$ecoval<100,]
> data0<-na.omit(data0)
> data0
CV11 CV12 CV13 CV14 CV15 CV21 CV22 CV23 CV24 CV25 CV26 CV31 CV32 CV33 CV41 CV42 CV43 CV44 CV51 CV52 IN11 IN12 IN13
4728 0 0 0 1 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
5339 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
11766 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
796 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3561 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
10581 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
10618 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 0 0 0 0
14376 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
14389 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0
790 0 0 0 1 0 0 0 0 1 0 0 2 0 0 0 0 0 0 0 0 1 0 0
3974 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
4739 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 1 0 0 0 0 0 0
156 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 99
5339 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 99
11766 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 99
796 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 98
3561 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 98
10581 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 98
10618 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 98
14376 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 98
14389 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 98
790 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 97
3974 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 97
4739 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 1 0 97
156 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 96
2740 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 96
2950 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 96
[ reached getOption("max.print") -- omitted 14984 rows ]
maybe something like this ?
library(dplyr)
data %>% group_by(ecoval > 100) %>% summarize_all(mean)
that should give you the average of each column for ecoval > and <= to 100

Select n rows after specific number

I work with a data.frame like this:
Country Date balance_of_payment business_confidence_indicator consumer_confidence_indicator CPI Crisis_IMF
1 Australia 1980-01-01 -0.87 100.215 99.780 25.4 0
2 Australia 1980-04-01 -1.62 100.061 99.746 26.2 0
3 Australia 1980-07-01 -3.70 100.599 100.049 26.6 0
4 Australia 1980-10-01 -3.13 100.597 100.735 27.2 0
5 Australia 1981-01-01 -2.73 101.149 101.016 27.8 0
6 Australia 1981-04-01 -4.11 100.936 100.150 28.4 0
I want to create a summary statistic with describe(dataset)from the HmiscPackage.
I need to differentiate between the timespans n-quarters before Crisis_IMF is 1, the time in which Crisis_IMF is 1 and the state n-quaters after Crisis_IMF is 1.
To select the time in which Crisis_IMF is 1, I did describe(dataset[dataset$Crisis_IMF==1,"balance_of_payment"]).
But I do not know how to make the command over the timespan of n-quarters (e.g. 8) after the event.
Edit:
dataset$Crisis_IMF
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[60] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[119] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[178] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[237] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[296] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[355] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[414] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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-3.70 0
4 Australia 1980-10-01 -3.13 0
5 Australia 1981-01-01 -2.73 0
6 Australia 1981-04-01 -4.11 0
7 Australia 1981-07-01 -3.98 0
8 Australia 1981-10-01 -5.27 0
9 Australia 1982-01-01 -5.31 0
10 Australia 1982-04-01 -4.67 0
11 Australia 1982-07-01 -3.30 0
12 Australia 1982-10-01 -3.24 0
13 Australia 1983-01-01 -3.45 0
14 Australia 1983-04-01 -2.86 0
15 Australia 1983-07-01 -3.58 0
...
137 Australia 2014-01-01 -2.18 0
138 Australia 2014-04-01 -3.44 0
139 Australia 2014-07-01 -3.04 0
140 Australia 2014-10-01 -2.39 0
141 Austria 1980-01-01 -3.97 0
142 Austria 1980-04-01 -3.89 0
143 Austria 1980-07-01 -1.84 0
144 Austria 1980-10-01 -1.60 0
145 Austria 1981-01-01 -2.74 0
146 Austria 1981-04-01 -2.88 0
147 Austria 1981-07-01 -2.83 0
148 Austria 1981-10-01 -2.06 0
149 Austria 1982-01-01 -0.63 0
150 Austria 1982-04-01 0.61 0
151 Austria 1982-07-01 2.42 0
152 Austria 1982-10-01 2.70 0
There can be more then one crisis period for one country. That e.g. in Australia are Crisis from 1990-01-01 to 1991-04-01 and 2002-01-01 to 2005-01-01. I want to create 3 different describe commands, which show the behaviour of the variable in the above mentioned states.
You haven't provided your full data, so I have to guess that your Crisis_IMF column has an unbroken sequence of zeroes (before the crisis), followed by an unbroken sequence of ones (during which the IMF crisis was considered to be in effect), finally followed by an unbroken sequence of zeroes (after the crisis).
Below I've synthesized my own data for testing. I only synthesized columns Crisis_IMF and balance_of_payment, because those appear to be the only columns relevant to your problem. I used 30 rows, with the first 10 before, the next 10 during, and the last 10 after the crisis. I used sort of a random parabolic arc for the balance_of_payment, but that was entirely random.
library('Hmisc');
set.seed(1);
N <- 30;
df <- data.frame(balance_of_payment=-5+2*seq(-1.5,1.5,len=N)^2+rnorm(N,0,0.2), Crisis_IMF=c(rep(0,N/3),rep(1,N/3),rep(0,N/3)) );
df;
## balance_of_payment Crisis_IMF
## 1 -0.6252908 0
## 2 -1.0625579 0
## 3 -1.8228927 0
## 4 -1.8503850 0
## 5 -2.5744076 0
## 6 -3.2324647 0
## 7 -3.3561408 0
## 8 -3.6484112 0
## 9 -3.9805631 0
## 10 -4.4136342 0
## 11 -4.2642312 1
## 12 -4.6598435 1
## 13 -4.9904788 1
## 14 -5.3947830 1
## 15 -4.7696630 1
## 16 -5.0036359 1
## 17 -4.9550811 1
## 18 -4.6774634 1
## 19 -4.5735679 1
## 20 -4.4478071 1
## 21 -4.1687610 0
## 22 -3.9392921 0
## 23 -3.7811631 0
## 24 -3.8514970 0
## 25 -2.9444058 0
## 26 -2.6515349 0
## 27 -2.2006002 0
## 28 -1.9499174 0
## 29 -1.1949166 0
## 30 -0.4164117 0
crisisRange <- range(which(df$Crisis_IMF==1));
crisisRange;
## [1] 11 20
df$Off_Crisis <- c((1-crisisRange[1]):-1,rep(0,diff(crisisRange)+1),1:(nrow(df)-crisisRange[2]));
df;
## balance_of_payment Crisis_IMF Off_Crisis
## 1 -0.6252908 0 -10
## 2 -1.0625579 0 -9
## 3 -1.8228927 0 -8
## 4 -1.8503850 0 -7
## 5 -2.5744076 0 -6
## 6 -3.2324647 0 -5
## 7 -3.3561408 0 -4
## 8 -3.6484112 0 -3
## 9 -3.9805631 0 -2
## 10 -4.4136342 0 -1
## 11 -4.2642312 1 0
## 12 -4.6598435 1 0
## 13 -4.9904788 1 0
## 14 -5.3947830 1 0
## 15 -4.7696630 1 0
## 16 -5.0036359 1 0
## 17 -4.9550811 1 0
## 18 -4.6774634 1 0
## 19 -4.5735679 1 0
## 20 -4.4478071 1 0
## 21 -4.1687610 0 1
## 22 -3.9392921 0 2
## 23 -3.7811631 0 3
## 24 -3.8514970 0 4
## 25 -2.9444058 0 5
## 26 -2.6515349 0 6
## 27 -2.2006002 0 7
## 28 -1.9499174 0 8
## 29 -1.1949166 0 9
## 30 -0.4164117 0 10
n <- 8;
describe(df[df$Off_Crisis>=-n&df$Off_Crisis<=-1,'balance_of_payment']);
## df[df$Off_Crisis >= -n & df$Off_Crisis <= -1, "balance_of_payment"]
## n missing unique Info Mean
## 8 0 8 1 -3.11
##
## -4.41363415781177 (1, 12%), -3.98056311135777 (1, 12%), -3.64841115885525 (1, 12%), -3.35614082447269 (1, 12%), -3.23246466374394 (1, 12%), -2.57440760140387 (1, 12%), -1.85038498107066 (1, 12%), -1.82289266659616 (1, 12%)
describe(df[df$Off_Crisis==0,'balance_of_payment']);
## df[df$Off_Crisis == 0, "balance_of_payment"]
## n missing unique Info Mean .05 .10 .25 .50 .75 .90 .95
## 10 0 10 1 -4.774 -5.219 -5.043 -4.982 -4.724 -4.595 -4.429 -4.347
##
## -5.39478302143074 (1, 10%), -5.00363594891363 (1, 10%), -4.99047879387293 (1, 10%), -4.95508109661503 (1, 10%), -4.76966304348196 (1, 10%), -4.67746343562751 (1, 10%), -4.65984348113626 (1, 10%), -4.57356788939893 (1, 10%), -4.44780713171369 (1, 10%), -4.26423116226702 (1, 10%)
describe(df[df$Off_Crisis>=1&df$Off_Crisis<=n,'balance_of_payment']);
## df[df$Off_Crisis >= 1 & df$Off_Crisis <= n, "balance_of_payment"]
## n missing unique Info Mean
## 8 0 8 1 -3.186
##
## -4.16876100605885 (1, 12%), -3.93929212154225 (1, 12%), -3.85149697413106 (1, 12%), -3.78116310320806 (1, 12%), -2.94440583734139 (1, 12%), -2.65153490367274 (1, 12%), -2.20060024283928 (1, 12%), -1.949917420894 (1, 12%)
The solution works by first computing the range of indexes during which the crisis was in effect as crisisRange. Then it appends to the data.frame a new column Off_Crisis which captures how many quarters offset from the crisis the row is, using negative numbers for before and positive numbers for after, and assuming each row represents exactly one quarter.
The describe() calls can then be made by subsetting on the Off_Crisis column, getting just the quarters offset from the crisis that you want for each call.
Edit: Whew! That was tough. Pretty sure I got it though:
library('Hmisc');
set.seed(1);
N <- 60;
df <- data.frame(balance_of_payment=rep(-5+2*seq(-1.5,1.5,len=N/2)^2,2)+rnorm(N,0,0.2), Crisis_IMF=c(rep(0,N/6),rep(1,N/6),rep(0,N/3),rep(1,N/6),rep(0,N/6)) );
df;
## balance_of_payment Crisis_IMF
## 1 -0.6252908 0
## 2 -1.0625579 0
## 3 -1.8228927 0
## 4 -1.8503850 0
## 5 -2.5744076 0
## 6 -3.2324647 0
## 7 -3.3561408 0
## 8 -3.6484112 0
## 9 -3.9805631 0
## 10 -4.4136342 0
## 11 -4.2642312 1
## 12 -4.6598435 1
## 13 -4.9904788 1
## 14 -5.3947830 1
## 15 -4.7696630 1
## 16 -5.0036359 1
## 17 -4.9550811 1
## 18 -4.6774634 1
## 19 -4.5735679 1
## 20 -4.4478071 1
## 21 -4.1687610 0
## 22 -3.9392921 0
## 23 -3.7811631 0
## 24 -3.8514970 0
## 25 -2.9444058 0
## 26 -2.6515349 0
## 27 -2.2006002 0
## 28 -1.9499174 0
## 29 -1.1949166 0
## 30 -0.4164117 0
## 31 -0.2282641 0
## 32 -1.1198441 0
## 33 -1.5782326 0
## 34 -2.1802021 0
## 35 -2.9157211 0
## 36 -3.1513699 0
## 37 -3.5324846 0
## 38 -3.8079388 0
## 39 -3.8757143 0
## 40 -4.1999213 0
## 41 -4.5994921 1
## 42 -4.7884845 1
## 43 -4.7268380 1
## 44 -4.8405104 1
## 45 -5.1324004 1
## 46 -5.1361483 1
## 47 -4.8789267 1
## 48 -4.7125241 1
## 49 -4.7602814 1
## 50 -4.3903659 1
## 51 -4.2729353 0
## 52 -4.2181247 0
## 53 -3.7278522 0
## 54 -3.6794993 0
## 55 -2.7817662 0
## 56 -2.2442292 0
## 57 -2.2428854 0
## 58 -1.8645939 0
## 59 -0.9853426 0
## 60 -0.5270109 0
df$Off_Crisis <- ifelse(df$Crisis_IMF==1,0,with(rle(df$Crisis_IMF),{ mids <- lengths[-c(1,length(lengths))]; c(-lengths[1]:-1,sequence(mids)-rep(rbind(0,mids+1),rbind(ceiling(mids/2),floor(mids/2))),1:lengths[length(lengths)]); }));
df;
## balance_of_payment Crisis_IMF Off_Crisis
## 1 -0.6252908 0 -10
## 2 -1.0625579 0 -9
## 3 -1.8228927 0 -8
## 4 -1.8503850 0 -7
## 5 -2.5744076 0 -6
## 6 -3.2324647 0 -5
## 7 -3.3561408 0 -4
## 8 -3.6484112 0 -3
## 9 -3.9805631 0 -2
## 10 -4.4136342 0 -1
## 11 -4.2642312 1 0
## 12 -4.6598435 1 0
## 13 -4.9904788 1 0
## 14 -5.3947830 1 0
## 15 -4.7696630 1 0
## 16 -5.0036359 1 0
## 17 -4.9550811 1 0
## 18 -4.6774634 1 0
## 19 -4.5735679 1 0
## 20 -4.4478071 1 0
## 21 -4.1687610 0 1
## 22 -3.9392921 0 2
## 23 -3.7811631 0 3
## 24 -3.8514970 0 4
## 25 -2.9444058 0 5
## 26 -2.6515349 0 6
## 27 -2.2006002 0 7
## 28 -1.9499174 0 8
## 29 -1.1949166 0 9
## 30 -0.4164117 0 10
## 31 -0.2282641 0 -10
## 32 -1.1198441 0 -9
## 33 -1.5782326 0 -8
## 34 -2.1802021 0 -7
## 35 -2.9157211 0 -6
## 36 -3.1513699 0 -5
## 37 -3.5324846 0 -4
## 38 -3.8079388 0 -3
## 39 -3.8757143 0 -2
## 40 -4.1999213 0 -1
## 41 -4.5994921 1 0
## 42 -4.7884845 1 0
## 43 -4.7268380 1 0
## 44 -4.8405104 1 0
## 45 -5.1324004 1 0
## 46 -5.1361483 1 0
## 47 -4.8789267 1 0
## 48 -4.7125241 1 0
## 49 -4.7602814 1 0
## 50 -4.3903659 1 0
## 51 -4.2729353 0 1
## 52 -4.2181247 0 2
## 53 -3.7278522 0 3
## 54 -3.6794993 0 4
## 55 -2.7817662 0 5
## 56 -2.2442292 0 6
## 57 -2.2428854 0 7
## 58 -1.8645939 0 8
## 59 -0.9853426 0 9
## 60 -0.5270109 0 10
n <- 8;
describe(df[df$Off_Crisis>=-n&df$Off_Crisis<=-1,'balance_of_payment']);
## df[df$Off_Crisis >= -n & df$Off_Crisis <= -1, "balance_of_payment"]
## n missing unique Info Mean .05 .10 .25 .50 .75 .90 .95
## 16 0 16 1 -3.133 -4.253 -4.090 -3.825 -3.294 -2.476 -1.837 -1.762
##
## -4.41363415781177 (1, 6%), -4.19992133068899 (1, 6%), -3.98056311135777 (1, 6%), -3.87571430729169 (1, 6%), -3.80793877922333 (1, 6%), -3.64841115885525 (1, 6%)
## -3.53248462570045 (1, 6%), -3.35614082447269 (1, 6%), -3.23246466374394 (1, 6%), -3.15136989958027 (1, 6%), -2.91572106713267 (1, 6%), -2.57440760140387 (1, 6%)
## -2.1802021496148 (1, 6%), -1.85038498107066 (1, 6%), -1.82289266659616 (1, 6%), -1.57823262180228 (1, 6%)
describe(df[df$Off_Crisis==0,'balance_of_payment']);
## df[df$Off_Crisis == 0, "balance_of_payment"]
## n missing unique Info Mean .05 .10 .25 .50 .75 .90 .95
## 20 0 20 1 -4.785 -5.149 -5.133 -4.964 -4.765 -4.645 -4.442 -4.384
##
## lowest : -5.395 -5.136 -5.132 -5.004 -4.990, highest: -4.599 -4.574 -4.448 -4.390 -4.264
describe(df[df$Off_Crisis>=1&df$Off_Crisis<=n,'balance_of_payment']);
## df[df$Off_Crisis >= 1 & df$Off_Crisis <= n, "balance_of_payment"]
## n missing unique Info Mean .05 .10 .25 .50 .75 .90 .95
## 16 0 16 1 -3.157 -4.232 -4.193 -3.873 -3.312 -2.244 -2.075 -1.929
##
## -4.27293530430708 (1, 6%), -4.21812466033862 (1, 6%), -4.16876100605885 (1, 6%), -3.93929212154225 (1, 6%), -3.85149697413106 (1, 6%), -3.78116310320806 (1, 6%)
## -3.72785216159621 (1, 6%), -3.67949925417454 (1, 6%), -2.94440583734139 (1, 6%), -2.78176624658013 (1, 6%), -2.65153490367274 (1, 6%), -2.24422917606577 (1, 6%)
## -2.24288543679152 (1, 6%), -2.20060024283928 (1, 6%), -1.949917420894 (1, 6%), -1.86459386937746 (1, 6%)
For this demo I synthesized five periods: 10 rows of non-crisis, 10 rows of crisis (the first), 20 rows of non-crisis, 10 rows of crisis (the second), and 10 rows of non-crisis. The algorithm is the same, namely to compute an Off_Crisis column (which was much more difficult this time!) and then use it to subset the data.frame for each describe() call. Only now, data points from different crises will be combined in the subsets.

3D Array value assignment ruins the structure of array

Here is how to reproduce my problem. I want to create a 3D array
> g=array(0,dim=c(3,31,31))
> dim(g)
[1] 3 31 31
> dim(g[1,,])
[1] 31 31
This is x with dimension 31 by 31
> dim(x)
[1] 31 31
> x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
1 NA 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0
2 0 NA 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
3 2 1 NA 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
4 0 0 0 NA 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
5 0 0 0 0 NA 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 NA 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0
7 0 0 0 0 0 1 NA 0 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 NA 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 NA 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
10 0 1 1 0 0 0 1 0 0 NA 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0
11 0 0 0 0 0 0 0 0 2 0 NA 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
12 0 0 0 0 0 0 1 1 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 NA 0 0 0
29 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 NA 0 0
30 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 NA 0
31 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 NA
when I try to assign x to the first 'section' of g using
> g[1,,] = x
The array structure of g is totally changed, as now it becomes:
> dim(g)
NULL
> head(g)
[[1]]
[1] NA 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0
[[2]]
[1] 0
[[3]]
[1] 0
[[4]]
[1] 0 NA 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
[[5]]
[1] 0
[[6]]
[1] 0
This is totally different from what I expected, I am just trying to put a matrix to g[1,,] and dim(g) should still be 3 by 31 by 31, am I wrong? where did I do wrong?
Thanks to Pascal's comments below I've amended my answer, though I've left the dimensionality changed to 31x31x3 for perhaps easier understanding. The problem is the way that the data are converted from your data.frame object before storing in your array. I think by converting first to a matrix you should get what you're looking for:
g <- array(0,dim=c(31,31,3))
m <- matrix(1, 31, 31)
x <- as.data.frame(m)
## Storing x as it is will result in g becoming a list...
#g[,,1] <- x
## Converting the data.frame to a matrix will result in
## g remaining an array:
g[,,1] <- as.matrix(x)

Resources