How to select a value from a table in R - r

I have the following data, called fit.2.sim:
An object of class "sim"
Slot "coef":
fit.2.sim
[,1] [,2]
[1,] -1.806363 5.148728
[2,] -3.599123 5.183769
[3,] 4.192562 4.855095
[4,] 2.658218 4.967007
[5,] -2.304084 5.220325
[6,] -1.010406 5.071663
[7,] 2.601671 5.129750
[8,] 5.977764 4.757826
[9,] 3.873432 4.932319
[10,] 1.281331 5.138091
Slot "sigma":
[1] 8.285497 10.659971 9.568340 8.649106 8.611894 9.041444 8.316859 7.990499 8.985450
[10] 7.947142
The command I have been using, to no avail unfortunately is:
fit.2.sim$coef[i,j]
i,j being the respective rows and columns. The error I get is:
"Error in fit.2.sim$coef : $ operator not defined for this S4 class"
Could you please tell me if there is another way to make this work?

S4 classes use # not $ to access slots, so you probably wanted
fit.t.sim#coef[i,j]

Related

How do I convert a Matrix in R to a Vector

Say I have the following matrix a (dput below):
> a
[,1] [,2] [,3] [,4]
[1,] 26.96318 136.3067 237.9886 399.4045
[2,] 55.36927 182.4738 246.5615 385.9003
[3,] 97.10758 122.8290 285.2592 325.1235
[4,] 80.23753 197.0916 203.1537 328.1359
[5,] 17.92519 109.8690 251.1775 328.4357
[6,] 71.45958 118.3602 249.6157 338.3364
[7,] 16.78765 161.3456 206.6488 385.8919
[8,] 32.80423 105.7197 234.8725 367.6513
[9,] 73.56880 108.8779 215.4797 316.6170
[10,] 41.08425 146.8893 211.2142 338.3543
How does one linearize(get a vector) it:
1. column by column
2. row by row
For example a column-by-column result would look like:
[1] 26.96318 55.36927 97.10758 80.23753 17.92519 71.45958 16.78765 32.80423 73.56880 41.08425 136.30665 182.47378
[13] 122.82902 197.09157 109.86903 118.36019 161.34561 105.71971 108.87791 146.88933 237.98864 246.56151 285.25918 203.15367
[25] 251.17751 249.61567 206.64882 234.87252 215.47969 211.21424 399.40453 385.90034 325.12346 328.13589 328.43573 338.33636
[37] 385.89189 367.65128 316.61704 338.35429
> dput(a)
structure(c(26.9631815841421, 55.369265563786, 97.1075813053176,
80.237529752776, 17.9251873865724, 71.4595773722976, 16.7876488063484,
32.8042338369414, 73.568799556233, 41.0842498764396, 136.306652054191,
182.473776396364, 122.829020931385, 197.091567260213, 109.869031514972,
118.360190931708, 161.345613677986, 105.719711235724, 108.877905877307,
146.889329864644, 237.988638831303, 246.561505645514, 285.259176045656,
203.153668926097, 251.177511387505, 249.615669064224, 206.648816983216,
234.872522787191, 215.479686786421, 211.214235564694, 399.404528317973,
385.90033929795, 325.123458285816, 328.135887836106, 328.43573493883,
338.336359220557, 385.891888826154, 367.651279014535, 316.617037914693,
338.354286877438), .Dim = c(10L, 4L))
as.vector() will convert your matrix into a vector column-by-column:
> as.vector(a)
[1] 26.96318 55.36927 97.10758 80.23753 17.92519 71.45958 16.78765
[8] 32.80423 73.56880 41.08425 136.30665 182.47378 122.82902 197.09157 ...
To get a row-by-row result, transpose it first, using t():
> as.vector(t(a))
[1] 26.96318 136.30665 237.98864 399.40453 55.36927 182.47378 246.56151
[8] 385.90034 97.10758 122.82902 285.25918 325.12346 80.23753 197.09157 ...

What are the equivalents of MCA variables coordinates and supplementary variables coordinates in mjca?

I would like to use mjca (package 'ca') on my data in order to estimate explained variation more realistically for the dimensions. The problem is that I would like to extract the coordinates of the active and supplementary variables in order to edit them in a data frame. However, the names of the variables and the dimensions are not given in the output of mjca. In MCA (package 'FactoMineR') the output is given as follows:
> mca$var$coord
Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
a 0.620468268 0.011534137 -0.542655702 0.47922448 0.15548571
cl 1.231043177 4.591555841 -0.323929172 0.19597918 -0.41446395
np -0.347646238 0.003735466 -0.006099464 0.02238883 0.16510343
num 0.417635652 -0.351884061 -0.760499677 0.60590774 -0.35647256
pr 0.945109906 -0.227098798 3.411969743 2.70823750 -0.64981046
vp 0.809895398 -0.303805822 0.048900811 -0.50023568 -0.53191069
EMB_no 0.396034450 -0.046768029 -0.058069978 0.05448188 0.06326411
EM_yes -1.009887848 0.119258474 0.148078445 -0.13892880 -0.16132349
ca -0.345163332 -0.088791765 -0.222907122 0.16679404 -0.12407031
to 0.375618920 0.096626332 0.242575397 -0.18151117 0.13501769
ART_no -0.006456155 0.021963298 0.049258256 -0.05919682 -0.07539649
ART_yes 0.044475732 -0.151302718 -0.339334655 0.40780032 0.51939806
> mca$quali.sup$coord
Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
ipva -0.1508708 0.04768873 -0.0233159 0.08795449 0.01645747
isv 0.6731160 -0.21276510 0.1040248 -0.39241234 -0.07342562
Is there a way to extract and paste the names of the variables to the coordinates in mjca? In mjca the output is not easily interpretable:
> mjca$colcoord
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] -1.14877950 -0.03284730 1.85933139 1.71512222 0.59783898 2.33329527 -1.96334559
[2,] -2.27924173 -13.07598521 1.10989653 0.70140040 -1.59360435 -0.53239520 0.59206669
[3,] 0.64365721 -0.01063798 0.02089893 0.08012859 0.63481889 -0.18685503 0.16878106
[4,] -0.77324063 1.00210711 2.60574231 2.16851574 -1.37062878 1.00967151 -7.97223158
[5,] -1.74984434 0.64673950 -11.69062156 9.69265663 -2.49850629 -0.05490287 -0.25510472
[6,] -1.49949849 0.86518831 -0.16755157 -1.79032033 -2.04518437 -0.20949668 0.65583755
[7,] -0.73324662 0.13318755 0.19896839 0.19498813 0.24324906 0.05337135 0.15195814
[8,] 1.86977887 -0.33962824 -0.50736940 -0.49721974 -0.62028511 -0.13609695 -0.38749326
[9,] 0.63906018 0.25286414 0.76375906 0.59694816 -0.47704750 -0.12352216 -0.44654719
[10,] -0.69544784 -0.27517568 -0.83114957 -0.64962006 0.51913993 0.13442117 0.48594841
[11,] 0.01195339 -0.06254781 -0.16877630 -0.21186268 -0.28989778 -0.27854165 -0.39280823
[12,] -0.08234556 0.43088491 1.16268119 1.45949845 1.99707361 1.91884250 2.70601223
[13,] -0.76304235 0.48124458 1.23544015 1.02426084 0.57083939 1.79253772 -0.83528819
[14,] -1.64355033 -5.87863606 0.76112230 0.59773026 -0.22298799 0.40707187 -0.23302386
[15,] -0.40920789 0.55803109 1.06737902 0.94054386 0.10598994 -0.60665661 -1.11993976
I think I should first extract the coordinates like this:
coord.mjca<-as.data.frame(mjca$colcoord)
row.names(coord.mjca)<-mjca$levelnames
colnames(coord.mjca)<-c("Dim 1", "Dim 2", "Dim 3", "Dim 4", "Dim 5", "Dim 6", "Dim 7")
Do you think I should do it like this?
Thank you for your help!
In ca package, you can use cacoord for this.
For example
cacoord(mca, type='rowprincipal', rows=T)
cacoord(mca, type='symmetric', cols=T)
I hope this helps.

Assigning variable names within a function in R

I am currently working on a dataset in R which is assigned to the global enviroment in R by a function of i, due to the nature of my work I am unable to disclose the dataset so let's use an example.
DATA
[,1] [,2] [,3] [,4] [,5]
[1,] 32320 27442 29275 45921 162306
[2,] 38506 29326 33290 45641 175386
[3,] 42805 30974 33797 47110 198358
[4,] 42107 34690 47224 62893 272305
[5,] 54448 39739 58548 69470 316550
[6,] 53358 48463 63793 79180 372685
Where DATA(i) is a function and the above is an output for a certain i
I want to assign variable names based on i such as:-
names(i)<-c(a(i),b(i),c(i),d(i),e(i))
for argument sake, let's say that the value of names for this specific i is
c("a","b","c","d","e")
I hope that it will produce the following:-
a b c d e
[1,] 32320 27442 29275 45921 162306
[2,] 38506 29326 33290 45641 175386
[3,] 42805 30974 33797 47110 198358
[4,] 42107 34690 47224 62893 272305
[5,] 54448 39739 58548 69470 316550
[6,] 53358 48463 63793 79180 372685
This is the code I currently use:-
VarName<-function(i){
colnames(DATA(i))<<-names(i)
}
However this produces an error message when I run it: "Error in colnames(DATA(i)) <- names(i)) :
target of assignment expands to non-language object" which we can see from my input that isn't true. Is there another way to do this?
Sorry for the basic questions. I'm fairly new to programming.

Read specific part of a data in R

I have this code here:
library(BCEA)
data(Vaccine)
ls()
Now, i get this:
[1] "c" "cost.GP" "cost.hosp" "cost.otc" "cost.time.off" "cost.time.vac"
[7] "cost.travel" "cost.trt1" "cost.trt2" "cost.vac" "e" "N"
[13] "N.outcomes" "N.resources" "QALYs.adv" "QALYs.death" "QALYs.hosp" "QALYs.inf"
[19] "QALYs.pne" "treats"
How can i access the 'c' value?
Something like Vaccine(c)?
Please help
In the given data Vaccine, each one of "c", "cost.GP", ... corresponds to separate data.
So, when you load the data of Vaccine into current workspace by data(Vaccine), you can directly access each of the 20 listed data values.
For example,
> head(c)
Status Quo Vaccination
[1,] 10.409146 16.252537
[2,] 5.834875 9.373437
[3,] 5.784903 15.935623
[4,] 12.208484 18.654250
[5,] 9.786787 16.467321
[6,] 6.560276 9.689887
Just like this:
c
It's a named object, so type "c" at the prompt, press return, or use directly in any function within this environment

tkplot in igraph within R

Here is my code with the corresponding output
> tkplot(g.2,vertex.label=nodes,
+ canvas.width=700,
+ canvas.height=700)
[1] 6
> ?tkplot
Warning message:
In rm(list = cmd, envir = .tkplot.env) : object 'tkp.6' not found
I get this error no matter what command I run after building and viewing my plot.
This may be obvious, but I can't get at the data from the plot.
> tkp.6.getcoords
Error: object 'tkp.6.getcoords' not found
Any thoughts? On Windows 2007 Pro.
R is a functional programming language. tkplot is a bit odd (for R users anyway) in that it returns numeric handles to its creations. Try this instead:
tkplot.getcoords(6)
When I run the example on the tkplot page, I then get this from tkplot.getcoords(1) since it was my first igraph plot:
> tkplot.getcoords(1)
[,1] [,2]
[1,] 334.49319 33.82983
[2,] 362.43837 286.10754
[3,] 410.61862 324.98319
[4,] 148.00673 370.91116
[5,] 195.69191 20.00000
[6,] 29.49197 430.00000
[7,] 20.00000 155.05409
[8,] 388.51103 62.61010
[9,] 430.00000 133.44695
[10,] 312.76239 168.90260

Resources