So, I have the following data.frame, and I want to generate two plots in one graph for yval vs. xval, for each zval and type tp. The lef
> df
xval yval se zval cond
1 1.0 1.831564e-02 1.831564e-03 0 a
2 1.2 2.705185e-02 2.705185e-03 0 a
3 1.4 3.916390e-02 3.916390e-03 0 a
4 1.6 5.557621e-02 5.557621e-03 0 a
5 1.8 7.730474e-02 7.730474e-03 0 a
6 2.0 1.053992e-01 1.053992e-02 0 a
7 2.2 1.408584e-01 1.408584e-02 0 a
8 2.4 1.845195e-01 1.845195e-02 0 a
9 2.6 2.369278e-01 2.369278e-02 0 a
10 2.8 2.981973e-01 2.981973e-02 0 a
11 3.0 3.678794e-01 3.678794e-02 0 a
12 3.2 4.448581e-01 4.448581e-02 0 a
13 3.4 5.272924e-01 5.272924e-02 0 a
14 3.6 6.126264e-01 6.126264e-02 0 a
15 3.8 6.976763e-01 6.976763e-02 0 a
16 4.0 7.788008e-01 7.788008e-02 0 a
17 4.2 8.521438e-01 8.521438e-02 0 a
18 4.4 9.139312e-01 9.139312e-02 0 a
19 4.6 9.607894e-01 9.607894e-02 0 a
20 4.8 9.900498e-01 9.900498e-02 0 a
21 5.0 1.000000e+00 1.000000e-01 0 a
22 5.2 9.900498e-01 9.900498e-02 0 a
23 5.4 9.607894e-01 9.607894e-02 0 a
24 5.6 9.139312e-01 9.139312e-02 0 a
25 5.8 8.521438e-01 8.521438e-02 0 a
26 6.0 7.788008e-01 7.788008e-02 0 a
27 6.2 6.976763e-01 6.976763e-02 0 a
28 6.4 6.126264e-01 6.126264e-02 0 a
29 6.6 5.272924e-01 5.272924e-02 0 a
30 6.8 4.448581e-01 4.448581e-02 0 a
31 7.0 3.678794e-01 3.678794e-02 0 a
32 7.2 2.981973e-01 2.981973e-02 0 a
33 7.4 2.369278e-01 2.369278e-02 0 a
34 7.6 1.845195e-01 1.845195e-02 0 a
35 7.8 1.408584e-01 1.408584e-02 0 a
36 8.0 1.053992e-01 1.053992e-02 0 a
37 8.2 7.730474e-02 7.730474e-03 0 a
38 8.4 5.557621e-02 5.557621e-03 0 a
39 8.6 3.916390e-02 3.916390e-03 0 a
40 8.8 2.705185e-02 2.705185e-03 0 a
41 9.0 1.831564e-02 1.831564e-03 0 a
42 9.2 1.215518e-02 1.215518e-03 0 a
43 9.4 7.907054e-03 7.907054e-04 0 a
44 9.6 5.041760e-03 5.041760e-04 0 a
45 9.8 3.151112e-03 3.151112e-04 0 a
46 10.0 1.930454e-03 1.930454e-04 0 a
47 1.0 3.726653e-06 7.453306e-07 0 b
48 1.2 9.929504e-06 1.985901e-06 0 b
49 1.4 2.541935e-05 5.083869e-06 0 b
50 1.6 6.252150e-05 1.250430e-05 0 b
51 1.8 1.477484e-04 2.954967e-05 0 b
52 2.0 3.354626e-04 6.709253e-05 0 b
53 2.2 7.318024e-04 1.463605e-04 0 b
54 2.4 1.533811e-03 3.067621e-04 0 b
55 2.6 3.088715e-03 6.177431e-04 0 b
56 2.8 5.976023e-03 1.195205e-03 0 b
57 3.0 1.110900e-02 2.221799e-03 0 b
58 3.2 1.984109e-02 3.968219e-03 0 b
59 3.4 3.404745e-02 6.809491e-03 0 b
60 3.6 5.613476e-02 1.122695e-02 0 b
61 3.8 8.892162e-02 1.778432e-02 0 b
62 4.0 1.353353e-01 2.706706e-02 0 b
63 4.2 1.978987e-01 3.957974e-02 0 b
64 4.4 2.780373e-01 5.560746e-02 0 b
65 4.6 3.753111e-01 7.506222e-02 0 b
66 4.8 4.867523e-01 9.735045e-02 0 b
67 5.0 6.065307e-01 1.213061e-01 0 b
68 5.2 7.261490e-01 1.452298e-01 0 b
69 5.4 8.352702e-01 1.670540e-01 0 b
70 5.6 9.231163e-01 1.846233e-01 0 b
71 5.8 9.801987e-01 1.960397e-01 0 b
72 6.0 1.000000e+00 2.000000e-01 0 b
73 6.2 9.801987e-01 1.960397e-01 0 b
74 6.4 9.231163e-01 1.846233e-01 0 b
75 6.6 8.352702e-01 1.670540e-01 0 b
76 6.8 7.261490e-01 1.452298e-01 0 b
77 7.0 6.065307e-01 1.213061e-01 0 b
78 7.2 4.867523e-01 9.735045e-02 0 b
79 7.4 3.753111e-01 7.506222e-02 0 b
80 7.6 2.780373e-01 5.560746e-02 0 b
81 7.8 1.978987e-01 3.957974e-02 0 b
82 8.0 1.353353e-01 2.706706e-02 0 b
83 8.2 8.892162e-02 1.778432e-02 0 b
84 8.4 5.613476e-02 1.122695e-02 0 b
85 8.6 3.404745e-02 6.809491e-03 0 b
86 8.8 1.984109e-02 3.968219e-03 0 b
87 9.0 1.110900e-02 2.221799e-03 0 b
88 9.2 5.976023e-03 1.195205e-03 0 b
89 9.4 3.088715e-03 6.177431e-04 0 b
90 9.6 1.533811e-03 3.067621e-04 0 b
91 9.8 7.318024e-04 1.463605e-04 0 b
92 10.0 3.354626e-04 6.709253e-05 0 b
93 1.0 6.065307e-01 1.819592e-01 1 a
94 1.2 7.261490e-01 2.178447e-01 1 a
95 1.4 8.352702e-01 2.505811e-01 1 a
96 1.6 9.231163e-01 2.769349e-01 1 a
97 1.8 9.801987e-01 2.940596e-01 1 a
98 2.0 1.000000e+00 3.000000e-01 1 a
99 2.2 9.801987e-01 2.940596e-01 1 a
100 2.4 9.231163e-01 2.769349e-01 1 a
101 2.6 8.352702e-01 2.505811e-01 1 a
102 2.8 7.261490e-01 2.178447e-01 1 a
103 3.0 6.065307e-01 1.819592e-01 1 a
104 3.2 4.867523e-01 1.460257e-01 1 a
105 3.4 3.753111e-01 1.125933e-01 1 a
106 3.6 2.780373e-01 8.341119e-02 1 a
107 3.8 1.978987e-01 5.936961e-02 1 a
108 4.0 1.353353e-01 4.060058e-02 1 a
109 4.2 8.892162e-02 2.667649e-02 1 a
110 4.4 5.613476e-02 1.684043e-02 1 a
111 4.6 3.404745e-02 1.021424e-02 1 a
112 4.8 1.984109e-02 5.952328e-03 1 a
113 5.0 1.110900e-02 3.332699e-03 1 a
114 5.2 5.976023e-03 1.792807e-03 1 a
115 5.4 3.088715e-03 9.266146e-04 1 a
116 5.6 1.533811e-03 4.601432e-04 1 a
117 5.8 7.318024e-04 2.195407e-04 1 a
118 6.0 3.354626e-04 1.006388e-04 1 a
119 6.2 1.477484e-04 4.432451e-05 1 a
120 6.4 6.252150e-05 1.875645e-05 1 a
121 6.6 2.541935e-05 7.625804e-06 1 a
122 6.8 9.929504e-06 2.978851e-06 1 a
123 7.0 3.726653e-06 1.117996e-06 1 a
124 7.2 1.343812e-06 4.031437e-07 1 a
125 7.4 4.655716e-07 1.396715e-07 1 a
126 7.6 1.549753e-07 4.649259e-08 1 a
127 7.8 4.956405e-08 1.486922e-08 1 a
128 8.0 1.522998e-08 4.568994e-09 1 a
129 8.2 4.496349e-09 1.348905e-09 1 a
130 8.4 1.275408e-09 3.826223e-10 1 a
131 8.6 3.475891e-10 1.042767e-10 1 a
132 8.8 9.101471e-11 2.730441e-11 1 a
133 9.0 2.289735e-11 6.869205e-12 1 a
134 9.2 5.534610e-12 1.660383e-12 1 a
135 9.4 1.285337e-12 3.856012e-13 1 a
136 9.6 2.867975e-13 8.603925e-14 1 a
137 9.8 6.148396e-14 1.844519e-14 1 a
138 10.0 1.266417e-14 3.799250e-15 1 a
139 1.0 2.096114e-01 1.676891e-02 1 b
140 1.2 2.664683e-01 2.131746e-02 1 b
141 1.4 3.320399e-01 2.656320e-02 1 b
142 1.6 4.055545e-01 3.244436e-02 1 b
143 1.8 4.855369e-01 3.884295e-02 1 b
144 2.0 5.697828e-01 4.558263e-02 1 b
145 2.2 6.554063e-01 5.243250e-02 1 b
146 2.4 7.389685e-01 5.911748e-02 1 b
147 2.6 8.166865e-01 6.533492e-02 1 b
148 2.8 8.847059e-01 7.077647e-02 1 b
149 3.0 9.394131e-01 7.515305e-02 1 b
150 3.2 9.777512e-01 7.822010e-02 1 b
151 3.4 9.975031e-01 7.980025e-02 1 b
152 3.6 9.975031e-01 7.980025e-02 1 b
153 3.8 9.777512e-01 7.822010e-02 1 b
154 4.0 9.394131e-01 7.515305e-02 1 b
155 4.2 8.847059e-01 7.077647e-02 1 b
156 4.4 8.166865e-01 6.533492e-02 1 b
157 4.6 7.389685e-01 5.911748e-02 1 b
158 4.8 6.554063e-01 5.243250e-02 1 b
159 5.0 5.697828e-01 4.558263e-02 1 b
160 5.2 4.855369e-01 3.884295e-02 1 b
161 5.4 4.055545e-01 3.244436e-02 1 b
162 5.6 3.320399e-01 2.656320e-02 1 b
163 5.8 2.664683e-01 2.131746e-02 1 b
164 6.0 2.096114e-01 1.676891e-02 1 b
165 6.2 1.616212e-01 1.292970e-02 1 b
166 6.4 1.221507e-01 9.772054e-03 1 b
167 6.6 9.049144e-02 7.239315e-03 1 b
168 6.8 6.571027e-02 5.256822e-03 1 b
169 7.0 4.677062e-02 3.741650e-03 1 b
170 7.2 3.263076e-02 2.610460e-03 1 b
171 7.4 2.231491e-02 1.785193e-03 1 b
172 7.6 1.495813e-02 1.196651e-03 1 b
173 7.8 9.828195e-03 7.862556e-04 1 b
174 8.0 6.329715e-03 5.063772e-04 1 b
175 8.2 3.995846e-03 3.196677e-04 1 b
176 8.4 2.472563e-03 1.978050e-04 1 b
177 8.6 1.499685e-03 1.199748e-04 1 b
178 8.8 8.915937e-04 7.132750e-05 1 b
179 9.0 5.195747e-04 4.156597e-05 1 b
180 9.2 2.967858e-04 2.374286e-05 1 b
181 9.4 1.661699e-04 1.329359e-05 1 b
182 9.6 9.119596e-05 7.295677e-06 1 b
183 9.8 4.905836e-05 3.924669e-06 1 b
184 10.0 2.586810e-05 2.069448e-06 1 b
I have used facet_grid to generate this plot, but there is one thing that I am trying to figure out. So, the right panel is for z=0, and the left is for z=1. I want to move the line legend to inside the left panel (for Z=1) (top corner). I couldn't find the option for that.
And here is my code that I used in R to generate the plot:
plot1 <- ggplot(data=df, aes(x=xval, y=yval, group=cond, colour=cond) ) +
+ geom_smooth(aes(ymin = yval-se, ymax = yval+se, linetype=cond, colour=cond, fill=cond), stat="identity", size=1.1) +
+ scale_colour_hue(l=25) +
+ ylim(-0.1,1.3) + scale_linetype_manual(values = c('a' = 1,'b' = 2))
plot1 + facet_grid(~ zval, scales="free_y") + theme(strip.text.x = element_blank(),strip.background = element_rect(colour="white", fill="white"))
plot1 <- ggplot(data=df, aes(x=xval, y=yval, group=cond, colour=cond) ) +
geom_smooth(aes(ymin = yval-se, ymax = yval+se,
linetype=cond, colour=cond, fill=cond), stat="identity",
size=1.1) +
scale_colour_hue(l=25) +
ylim(-0.1,1.3) + scale_linetype_manual(values = c('a' = 1,'b' = 2))
The coordinates for legend.position are x- and y- offsets from the bottom-left of the plot, ranging from 0 - 1.
plot1 + facet_grid(~ zval, scales="free_y") +
theme(strip.text.x = element_blank(),
strip.background = element_rect(colour="white", fill="white"),
legend.position=c(.9,.75)
)
Tweak the legend.position values to suit your preference.
Related
I've got a (pretty simple) code to download a table with data:
library(rvest)
link = "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/team/2442/statistics"
aguada = read_html(link)
stats = aguada %>% html_nodes("tbody")
stats = aguada %>% html_nodes(xpath="/html/body/div[1]/div[6]/div/div/div/div[4]/table") %>% html_table()
my_df <- as.data.frame(stats)
And now I'm trying to do the same, but for the URLs for each player in the same table
for (i in 1:17){
url_path="/html/body/div[1]/div[6]/div/div/div/div[4]/table/tbody/tr[i]/td[1]/a"
jugador[i] = aguada %>% html_nodes(xpath=url_path)%>% html_attr("href")
}
I've tried the code above, and while it doesn't crash, it doesn't work as intended either. I want to create an array with the urls or something like that so I can then get the stats for each player easily. While we're at it, I'd like to know if, instead of doing 1:17 in the for and manually counting the players, there's a way to automate that too, so I can do something like for i in 1:table_length
You need to initialise the vector jugador to be able to append the links to it. Also, when you create a path that invloves changing a character within the path, paste concatenates the strings with the number i to create the path, as shown below:
jugador <- vector()
for(i in 1:17){
url_path <- paste("/html/body/div[1]/div[6]/div/div/div/div[4]/table/tbody/tr[", i, "]/td[1]/a", sep = "")
jugador[i] <- aguada %>% html_nodes(xpath=url_path)%>% html_attr("href")
}
Result:
> jugador
[1] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/15257?"
[2] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/17101?"
[3] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/17554?"
[4] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/43225?"
[5] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/262286?"
[6] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/623893?"
[7] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/725720?"
[8] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/858052?"
[9] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/1645559?"
[10] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/1651515?"
[11] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/1717089?"
[12] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/1924883?"
[13] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/1924884?"
[14] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/1931124?"
[15] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/1950388?"
[16] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/1971299?"
[17] "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/person/1991297?"
Links in the last column. Without loop
library(tidyverse)
library(rvest)
page <-
"https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/team/2442/statistics" %>%
read_html()
df <- page %>%
html_table() %>%
pluck(1) %>%
janitor::clean_names() %>%
mutate(link = page %>%
html_elements("td a") %>%
html_attr("href") %>%
unique())
# A tibble: 17 x 21
jugador p i pts_pr pts as_pr as ro_pr rd_pr rt_pr rt bl_prom bl re_pr re min_pr tc_percent x2p_percent x3p_percent tl_percent link$value
<chr> <int> <int> <dbl> <int> <dbl> <int> <dbl> <dbl> <dbl> <int> <dbl> <int> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 F. MEDINA 22 9 6 131 1.3 29 0.5 0.8 1.3 28 0 0 0.6 13 22 37 55.6 26.8 60 https://hosted.dcd.share~
2 J. SANTISO 23 23 12 277 5.6 128 0.4 2.9 3.3 75 0 0 0.7 15 31 43.1 43.2 43 75 https://hosted.dcd.share~
3 A. ZUVICH 17 1 8.2 139 0.7 11 2 2.9 4.9 83 0.5 8 1.1 19 15.9 59.8 67.1 16.7 76.5 https://hosted.dcd.share~
4 A. YOUNG 15 14 12.5 187 1.3 20 0.4 3.3 3.7 55 0.5 7 0.6 9 30.5 36.2 41.9 32 78.8 https://hosted.dcd.share~
5 E. VARGAS 23 23 16.1 370 1.9 44 3.5 8.4 11.9 273 1.6 37 1.1 25 30.3 53.3 53.5 0 62.6 https://hosted.dcd.share~
6 L. PLANELLS 23 0 3.6 83 1.6 37 0.5 1.1 1.6 37 0.1 2 0.7 17 15.1 35.4 35.1 35.6 90 https://hosted.dcd.share~
7 T. METZGER 11 9 6.8 75 0.6 7 1.7 3.3 5 55 0.4 4 0.5 5 23.1 37 44.2 28.9 40 https://hosted.dcd.share~
8 L. SILVA 19 0 1.1 21 0.1 2 0.2 0.2 0.3 6 0.1 1 0 0 4 35 71.4 15.4 100 https://hosted.dcd.share~
9 J. STOLL 2 0 0 0 0 0 0 0 0 0 0 0 0 0 1.2 0 0 0 0 https://hosted.dcd.share~
10 G. BRUN 4 0 0.8 3 0 0 0.3 0 0.3 1 0 0 0 0 0.6 50 0 50 0 https://hosted.dcd.share~
11 A. GENTILE 3 0 0 0 0 0 0.3 0.3 0.7 2 0 0 0 0 1 0 0 0 0 https://hosted.dcd.share~
12 L. CERMINATO 19 5 8.6 163 1.7 33 1.3 3.6 4.9 93 0.7 14 0.9 17 20.9 44.1 51.9 27.1 57.1 https://hosted.dcd.share~
13 J. ADAMS 8 8 16.6 133 1.9 15 1 2.5 3.5 28 0.3 2 1.9 15 28.9 46.2 53.9 26.7 81.8 https://hosted.dcd.share~
14 K. FULLER 5 5 4.6 23 1.8 9 0.6 0.6 1.2 6 0 0 0.4 2 20.1 17.1 0 28.6 83.3 https://hosted.dcd.share~
15 S. MAC 4 4 12.5 50 2 8 0 3 3 12 0.5 2 1.8 7 29.9 37.8 35.5 42.9 76.9 https://hosted.dcd.share~
16 O. JOHNSON 12 12 15.4 185 3.4 41 1 3.2 4.2 50 0.3 4 0.8 9 31.8 47.3 53.6 34.7 75 https://hosted.dcd.share~
17 G. SOLANO 2 2 15.5 31 6.5 13 0.5 5.5 6 12 0 0 1 2 32.4 41.4 55.6 18.2 71.4 https://hosted.dcd.share~
Inside the string, i is just a regular character, and XPath doesn’t know it: it has no connection to the variables in your R session.
However, if you want to select all elements with a given XPath, you don’t need the index at all. That is, the following XPath expression works (I’ve simply removed the [i] part):
/html/body/div[1]/div[6]/div/div/div/div[4]/table/tbody/tr/td[1]/a
Here’s the corresponding ‘rvest’ code. Note that it uses no loop:
library(rvest)
link = "https://hosted.dcd.shared.geniussports.com/fubb/es/competition/34409/team/2442/statistics"
aguada = read_html(link)
jugador = aguada %>%
html_nodes(xpath = "/html/body/div[1]/div[6]/div/div/div/div[4]/table/tbody/tr/td[1]/a/#href")
Or, alternatively:
jugador = aguada %>%
html_nodes(xpath = "/html/body/div[1]/div[6]/div/div/div/div[4]/table/tbody/tr/td[1]/a") %>%
html_attr("href")
Both return a vector of hyperrefs. The first solution has a slightly different return type (xml_nodeset) but for most purposes they will be similar.
This question already has answers here:
Categorize numeric variable into group/ bins/ breaks
(4 answers)
Closed 1 year ago.
I am attempting to add a new column to the state sample data frame in R. I am hoping for this column to cluster the ID of states into broader categories (1-4). My code is close to what I am looking for but I am not getting it quite right.. I know I could enter each state ID line by line but is there a a quicker way? Thank you!
library(tidyverse)
#Add column to denote each state
States=state.x77
States=data.frame(States)
States <- tibble::rowid_to_column(States, "ID")
States
#Create new variable for state buckets
States <- States %>%
mutate(WAGE_BUCKET=case_when(ID <= c(1,12) ~ '1',
ID <= c(13,24) ~ '2',
ID <= c(25,37) ~ '3',
ID <= c(38,50) ~ '4',
TRUE ~ 'NA'))
View(States) #It is not grouping the states in the way I want/I am still getting some NA values but unsure why!
You can use cut or findInterval if all of your groups will be using contiguous ID values:
findInterval(States$ID, c(0, 12, 24, 37, 51))
# [1] 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4
If you want to make it a bit more verbose, you can use dplyr::between in your case_when:
States %>%
mutate(
WAGE_BUCKET = case_when(
between(ID, 1, 12) ~ "1",
between(ID, 13, 24) ~ "2",
between(ID, 25, 37) ~ "3",
between(ID, 38, 50) ~ "4",
TRUE ~ NA_character_)
)
# ID Population Income Illiteracy Life Exp Murder HS Grad Frost Area WAGE_BUCKET
# 1 1 3615 3624 2.1 69.05 15.1 41.3 20 50708 1
# 2 2 365 6315 1.5 69.31 11.3 66.7 152 566432 1
# 3 3 2212 4530 1.8 70.55 7.8 58.1 15 113417 1
# 4 4 2110 3378 1.9 70.66 10.1 39.9 65 51945 1
# 5 5 21198 5114 1.1 71.71 10.3 62.6 20 156361 1
# 6 6 2541 4884 0.7 72.06 6.8 63.9 166 103766 1
# 7 7 3100 5348 1.1 72.48 3.1 56.0 139 4862 1
# 8 8 579 4809 0.9 70.06 6.2 54.6 103 1982 1
# 9 9 8277 4815 1.3 70.66 10.7 52.6 11 54090 1
# 10 10 4931 4091 2.0 68.54 13.9 40.6 60 58073 1
# 11 11 868 4963 1.9 73.60 6.2 61.9 0 6425 1
# 12 12 813 4119 0.6 71.87 5.3 59.5 126 82677 1
# 13 13 11197 5107 0.9 70.14 10.3 52.6 127 55748 2
# 14 14 5313 4458 0.7 70.88 7.1 52.9 122 36097 2
# 15 15 2861 4628 0.5 72.56 2.3 59.0 140 55941 2
# 16 16 2280 4669 0.6 72.58 4.5 59.9 114 81787 2
# 17 17 3387 3712 1.6 70.10 10.6 38.5 95 39650 2
# 18 18 3806 3545 2.8 68.76 13.2 42.2 12 44930 2
# 19 19 1058 3694 0.7 70.39 2.7 54.7 161 30920 2
# 20 20 4122 5299 0.9 70.22 8.5 52.3 101 9891 2
# 21 21 5814 4755 1.1 71.83 3.3 58.5 103 7826 2
# 22 22 9111 4751 0.9 70.63 11.1 52.8 125 56817 2
# 23 23 3921 4675 0.6 72.96 2.3 57.6 160 79289 2
# 24 24 2341 3098 2.4 68.09 12.5 41.0 50 47296 2
# 25 25 4767 4254 0.8 70.69 9.3 48.8 108 68995 3
# 26 26 746 4347 0.6 70.56 5.0 59.2 155 145587 3
# 27 27 1544 4508 0.6 72.60 2.9 59.3 139 76483 3
# 28 28 590 5149 0.5 69.03 11.5 65.2 188 109889 3
# 29 29 812 4281 0.7 71.23 3.3 57.6 174 9027 3
# 30 30 7333 5237 1.1 70.93 5.2 52.5 115 7521 3
# 31 31 1144 3601 2.2 70.32 9.7 55.2 120 121412 3
# 32 32 18076 4903 1.4 70.55 10.9 52.7 82 47831 3
# 33 33 5441 3875 1.8 69.21 11.1 38.5 80 48798 3
# 34 34 637 5087 0.8 72.78 1.4 50.3 186 69273 3
# 35 35 10735 4561 0.8 70.82 7.4 53.2 124 40975 3
# 36 36 2715 3983 1.1 71.42 6.4 51.6 82 68782 3
# 37 37 2284 4660 0.6 72.13 4.2 60.0 44 96184 3
# 38 38 11860 4449 1.0 70.43 6.1 50.2 126 44966 4
# 39 39 931 4558 1.3 71.90 2.4 46.4 127 1049 4
# 40 40 2816 3635 2.3 67.96 11.6 37.8 65 30225 4
# 41 41 681 4167 0.5 72.08 1.7 53.3 172 75955 4
# 42 42 4173 3821 1.7 70.11 11.0 41.8 70 41328 4
# 43 43 12237 4188 2.2 70.90 12.2 47.4 35 262134 4
# 44 44 1203 4022 0.6 72.90 4.5 67.3 137 82096 4
# 45 45 472 3907 0.6 71.64 5.5 57.1 168 9267 4
# 46 46 4981 4701 1.4 70.08 9.5 47.8 85 39780 4
# 47 47 3559 4864 0.6 71.72 4.3 63.5 32 66570 4
# 48 48 1799 3617 1.4 69.48 6.7 41.6 100 24070 4
# 49 49 4589 4468 0.7 72.48 3.0 54.5 149 54464 4
# 50 50 376 4566 0.6 70.29 6.9 62.9 173 97203 4
It is a vector of length > 1. The comparison operators works on a single vector. We could use between
library(dplyr)
States <- States %>%
mutate(WAGE_BUCKET=case_when(between(ID, 1, 12) ~ '1',
between(ID, 13,24) ~ '2',
between(ID, 25,37) ~ '3',
between(ID, 38,50) ~ '4',
TRUE ~ NA_character_))
Or another option is to use & with > and <=
States %>%
mutate(WAGE_BUCKET=case_when(ID >= 1 & ID <=12 ~ '1',
ID >= 13 & ID <= 24) ~ '2',
ID >= 25 & ID <= 37 ~ '3',
ID >= 38 & ID <= 50 ~ '4',
TRUE ~ NA_character))
Or may be the OP meant to use %in%
States %>%
mutate(WAGE_BUCKET=case_when(ID %in% c(1,12) ~ '1',
ID %in% c(13,24) ~ '2',
ID %in% c(25,37) ~ '3',
ID %in% c(38,50) ~ '4',
TRUE ~ NA_character_))
I am doing a meta-analysis in R. For each study (variable StudyID) I have multiple effect sizes. For some studies I have the same effect size multiple times depending on the level of acquaintance (variable Familiarity) between the subjects.
head(dat)
studyID A.C.Extent Visibility Familiarity p_t_cov group.size same.sex N published
1 1 3.0 5.0 1 0.0462 4 0 44 1
2 1 5.0 2.5 1 0.1335 4 0 44 1
3 1 2.5 3.0 1 -0.1239 4 0 44 1
4 1 2.5 3.5 1 0.2062 4 0 44 1
5 1 2.5 3.0 1 -0.0370 4 0 44 1
6 1 3.0 5.0 1 -0.3850 4 0 44 1
Those are the first rows of the data set. In total there are over 50 studies. Most studies look like study 1 with the same value in "Familiarity" for all effect sizes. In some studies, there are effect sizes with multiple levels of familiarity. For example study 36 as seen below.
head(dat)
studyID A.C.Extent Visibility Familiarity p_t_cov group.size same.sex N published
142 36 1.0 4.5 0 0.1233 5.00 0 311 1
143 36 3.5 3.0 0 0.0428 5.00 0 311 1
144 36 1.0 4.5 0 0.0986 5.00 0 311 1
145 36 1.0 4.5 1 -0.0520 5.00 0 311 1
146 36 1.5 2.5 1 -0.0258 5.00 0 311 1
147 36 3.5 3.0 1 0.1104 5.00 0 311 1
148 36 1.0 4.5 1 0.0282 5.00 0 311 1
149 36 1.0 4.5 2 -0.1724 5.00 0 311 1
150 36 3.5 3.0 2 0.2646 5.00 0 311 1
151 36 1.0 4.5 2 -0.1426 5.00 0 311 1
152 37 3.0 4.0 1 0.0118 5.35 0 123 0
153 37 1.0 4.5 1 -0.3205 5.35 0 123 0
154 37 2.5 3.0 1 -0.2356 5.35 0 123 0
155 37 3.0 2.0 1 0.1372 5.35 0 123 0
156 37 2.5 2.5 1 -0.1401 5.35 0 123 0
157 37 3.0 3.5 1 -0.3334 5.35 0 123 0
158 37 2.5 2.5 1 0.0317 5.35 0 123 0
159 37 1.0 3.0 1 -0.3025 5.35 0 123 0
160 37 1.0 3.5 1 -0.3248 5.35 0 123 0
Now I want for those studies that include multiple levels of familiarity, to take the rows with only one level of familiarity (two seperate versions: one with the lower, one with the higher familiarity).
I think that it can be possible with the package dplyr, but I have no real code so far.
In a second step I would like to give those rows unique studyIDs for each level of familiarity (so create out of study 36 three "different" studies).
Thank you in advance!
If you want to use dplyr, you could create an alternate ID or casenum by using group_indices:
df <- df %>%
mutate(case_num = group_indices(.dots=c("studyID", "Familiarity")))
You could do:
library(dplyr)
df %>%
group_by(studyID) %>%
mutate(nDist = n_distinct(Familiarity) > 1) %>%
ungroup() %>%
mutate(
studyID = case_when(nDist ~ paste(studyID, Familiarity, sep = "_"), TRUE ~ studyID %>% as.character),
nDist = NULL
)
Output:
# A tibble: 19 x 9
studyID A.C.Extent Visibility Familiarity p_t_cov group.size same.sex N published
<chr> <dbl> <dbl> <int> <dbl> <dbl> <int> <int> <int>
1 36_0 1 4.5 0 0.123 5 0 311 1
2 36_0 3.5 3 0 0.0428 5 0 311 1
3 36_0 1 4.5 0 0.0986 5 0 311 1
4 36_1 1 4.5 1 -0.052 5 0 311 1
5 36_1 1.5 2.5 1 -0.0258 5 0 311 1
6 36_1 3.5 3 1 0.110 5 0 311 1
7 36_1 1 4.5 1 0.0282 5 0 311 1
8 36_2 1 4.5 2 -0.172 5 0 311 1
9 36_2 3.5 3 2 0.265 5 0 311 1
10 36_2 1 4.5 2 -0.143 5 0 311 1
11 37 3 4 1 0.0118 5.35 0 123 0
12 37 1 4.5 1 -0.320 5.35 0 123 0
13 37 2.5 3 1 -0.236 5.35 0 123 0
14 37 3 2 1 0.137 5.35 0 123 0
15 37 2.5 2.5 1 -0.140 5.35 0 123 0
16 37 3 3.5 1 -0.333 5.35 0 123 0
17 37 2.5 2.5 1 0.0317 5.35 0 123 0
18 37 1 3 1 -0.302 5.35 0 123 0
19 37 1 3.5 1 -0.325 5.35 0 123 0
I have an output.txt file that results from an analysis I am doing in R. I would like to extract:
The << Output >> tables from the .txt file for each subject and combine them into an R data frame. The output column names are consistent between each of the subjects.
The same for the << Predicted Output >> and combine into a data frame in R.
As noted, the output file has written text in between that I don't need.
To make it easy for looking into the structure of the output.txt file, I have uploaded the .txt file on the following link here. I am also putting a screenshot below to show how the output is structured.
I attempted to do this using things like this but no luck:
df <- read.delim("ivivc_outputs.txt").
Try this to get started. Add more conditions if you need. I hope, this is helpful. If you need anything feel free to ask.
b = readLines(file('ivivc_outputs.txt', 'r'))
n_out = 1
n_pred = 1
listOutput = list()
listPredictOutput = list()
for(i in 1:length(b)){
if(b[i] == "<< Output >>"){
a = strsplit(b[i+1], " ")[[1]]
a = a[a != ""]
# print(a)
df <- data.frame(matrix(ncol = length(a), nrow = 0))
colnames(df) = a
control = 2
while(control != 20){
l = strsplit(b[i+control], " ")[[1]]
l = l[l != ""]
df[control-1,] = l
control = control + 1
}
listOutput[[n_out]] = df
n_out = n_out+1
}
if(b[i] == "<< Predicted Output >>"){
a = strsplit(b[i+1], " ")[[1]]
a = a[a != ""]
# print(a)
df <- data.frame(matrix(ncol = length(a), nrow = 0))
colnames(df) = a
control = 2
while(control != 20){
l = strsplit(b[i+control], " ")[[1]]
l = l[l != ""]
df[control-1,] = l
control = control + 1
}
listPredictOutput[[n_pred]] = df
n_pred = n_pred+1
}
}
# to merge all data frames use `bind_rows` from `dplyr`
library(dplyr)
dfOutput = bind_rows(listOutput)
dfPredictOutput = bind_rows(listPredictOutput)
> dfPredictOutput
# pH subj formula. time FABpred conc.pred AUCpred
# 1 1.2 1 capsule 0.0 0.000000 0.00000 0.00000
# 2 1.2 1 capsule 1.0 2.528737 8.39300 4.19650
# 3 1.2 1 capsule 2.0 7.415708 22.57987 19.68293
# 4 1.2 1 capsule 3.0 15.845734 45.08950 53.51761
# 5 1.2 1 capsule 4.0 24.275759 62.14611 107.13542
# 6 1.2 1 capsule 5.0 33.133394 76.48998 176.45346
# 7 1.2 1 capsule 6.0 41.991029 87.35901 258.37796
# 8 1.2 1 capsule 6.5 45.900606 89.90799 302.69471
# 9 1.2 1 capsule 7.0 49.810183 92.12832 348.20378
# 10 1.2 1 capsule 7.5 53.719760 94.06237 394.75146
# 11 1.2 1 capsule 8.0 57.629337 95.74705 442.20381
# 12 1.2 1 capsule 8.0 57.629337 95.74705 442.20381
# 13 1.2 1 capsule 9.0 63.860225 93.23271 536.69369
# 14 1.2 1 capsule 10.0 70.091113 91.32747 628.97378
# 15 1.2 1 capsule 12.0 79.498532 79.70975 800.01101
# 16 1.2 1 capsule 16.0 90.372043 49.52751 1058.48553
# 17 1.2 1 capsule 20.0 101.245554 40.79704 1239.13462
# 18 1.2 1 capsule 24.0 107.354268 26.67212 1374.07293
# 19 1.2 1 capsuleContent 0.0 0.000000 0.000000 0.000000
# 20 1.2 1 capsuleContent 1.0 1.490256 4.946231 2.473115
# 21 1.2 1 capsuleContent 2.0 5.338746 16.521316 13.206889
# 22 1.2 1 capsuleContent 3.0 13.341161 39.079386 41.007240
# 23 1.2 1 capsuleContent 4.0 21.343576 56.172708 88.633287
# 24 1.2 1 capsuleContent 5.0 30.017950 71.355393 152.397337
# 25 1.2 1 capsuleContent 6.0 38.692324 82.860036 229.505052
# 26 1.2 1 capsuleContent 6.5 42.571357 85.881180 271.690356
# 27 1.2 1 capsuleContent 7.0 46.450390 88.512793 315.288850
# 28 1.2 1 capsuleContent 7.5 50.329423 90.805099 360.118323
# 29 1.2 1 capsuleContent 8.0 54.208457 92.801847 406.020060
# 30 1.2 1 capsuleContent 8.0 54.208457 92.801847 406.020060
# 31 1.2 1 capsuleContent 9.0 60.439345 91.000989 497.921478
# 32 1.2 1 capsuleContent 10.0 66.670233 89.636393 588.240168
# 33 1.2 1 capsuleContent 12.0 76.322001 79.472871 757.349432
# 34 1.2 1 capsuleContent 16.0 87.256599 49.607701 1015.510577
# 35 1.2 1 capsuleContent 20.0 98.191197 40.968947 1196.663873
# 36 1.2 1 capsuleContent 24.0 104.177736 26.424419 1331.450604
# 37 1.2 2 capsule 0.0 0.000000 0.00000 0.00000
# 38 1.2 2 capsule 1.0 2.528737 8.39300 4.19650
# 39 1.2 2 capsule 2.0 7.415708 22.57987 19.68293
# 40 1.2 2 capsule 3.0 15.845734 45.08950 53.51761
# 41 1.2 2 capsule 4.0 24.275759 62.14611 107.13542
# 42 1.2 2 capsule 5.0 33.133394 76.48998 176.45346
# 43 1.2 2 capsule 6.0 41.991029 87.35901 258.37796
# 44 1.2 2 capsule 6.5 45.900606 89.90799 302.69471
# 45 1.2 2 capsule 7.0 49.810183 92.12832 348.20378
# 46 1.2 2 capsule 7.5 53.719760 94.06237 394.75146
# 47 1.2 2 capsule 8.0 57.629337 95.74705 442.20381
# 48 1.2 2 capsule 8.0 57.629337 95.74705 442.20381
# 49 1.2 2 capsule 9.0 63.860225 93.23271 536.69369
# 50 1.2 2 capsule 10.0 70.091113 91.32747 628.97378
# 51 1.2 2 capsule 12.0 79.498532 79.70975 800.01101
# 52 1.2 2 capsule 16.0 90.372043 49.52751 1058.48553
# 53 1.2 2 capsule 20.0 101.245554 40.79704 1239.13462
# 54 1.2 2 capsule 24.0 107.354268 26.67212 1374.07293
# 55 1.2 2 capsuleContent 0.0 0.000000 0.000000 0.000000
# 56 1.2 2 capsuleContent 1.0 1.490256 4.946231 2.473115
# 57 1.2 2 capsuleContent 2.0 5.338746 16.521316 13.206889
# 58 1.2 2 capsuleContent 3.0 13.341161 39.079386 41.007240
# 59 1.2 2 capsuleContent 4.0 21.343576 56.172708 88.633287
# 60 1.2 2 capsuleContent 5.0 30.017950 71.355393 152.397337
# 61 1.2 2 capsuleContent 6.0 38.692324 82.860036 229.505052
# 62 1.2 2 capsuleContent 6.5 42.571357 85.881180 271.690356
# 63 1.2 2 capsuleContent 7.0 46.450390 88.512793 315.288850
# 64 1.2 2 capsuleContent 7.5 50.329423 90.805099 360.118323
# 65 1.2 2 capsuleContent 8.0 54.208457 92.801847 406.020060
# 66 1.2 2 capsuleContent 8.0 54.208457 92.801847 406.020060
# 67 1.2 2 capsuleContent 9.0 60.439345 91.000989 497.921478
# 68 1.2 2 capsuleContent 10.0 66.670233 89.636393 588.240168
# 69 1.2 2 capsuleContent 12.0 76.322001 79.472871 757.349432
# 70 1.2 2 capsuleContent 16.0 87.256599 49.607701 1015.510577
# 71 1.2 2 capsuleContent 20.0 98.191197 40.968947 1196.663873
# 72 1.2 2 capsuleContent 24.0 104.177736 26.424419 1331.450604
# 73 1.2 3 capsule 0.0 0.000000 0.00000 0.00000
# 74 1.2 3 capsule 1.0 2.528737 8.39300 4.19650
# 75 1.2 3 capsule 2.0 7.415708 22.57987 19.68293
# 76 1.2 3 capsule 3.0 15.845734 45.08950 53.51761
# 77 1.2 3 capsule 4.0 24.275759 62.14611 107.13542
# 78 1.2 3 capsule 5.0 33.133394 76.48998 176.45346
# 79 1.2 3 capsule 6.0 41.991029 87.35901 258.37796
# 80 1.2 3 capsule 6.5 45.900606 89.90799 302.69471
# 81 1.2 3 capsule 7.0 49.810183 92.12832 348.20378
# 82 1.2 3 capsule 7.5 53.719760 94.06237 394.75146
# 83 1.2 3 capsule 8.0 57.629337 95.74705 442.20381
# 84 1.2 3 capsule 8.0 57.629337 95.74705 442.20381
# 85 1.2 3 capsule 9.0 63.860225 93.23271 536.69369
# 86 1.2 3 capsule 10.0 70.091113 91.32747 628.97378
# 87 1.2 3 capsule 12.0 79.498532 79.70975 800.01101
# 88 1.2 3 capsule 16.0 90.372043 49.52751 1058.48553
# 89 1.2 3 capsule 20.0 101.245554 40.79704 1239.13462
# 90 1.2 3 capsule 24.0 107.354268 26.67212 1374.07293
# 91 1.2 3 capsuleContent 0.0 0.000000 0.000000 0.000000
# 92 1.2 3 capsuleContent 1.0 1.490256 4.946231 2.473115
# 93 1.2 3 capsuleContent 2.0 5.338746 16.521316 13.206889
# 94 1.2 3 capsuleContent 3.0 13.341161 39.079386 41.007240
# 95 1.2 3 capsuleContent 4.0 21.343576 56.172708 88.633287
# 96 1.2 3 capsuleContent 5.0 30.017950 71.355393 152.397337
# 97 1.2 3 capsuleContent 6.0 38.692324 82.860036 229.505052
# 98 1.2 3 capsuleContent 6.5 42.571357 85.881180 271.690356
# 99 1.2 3 capsuleContent 7.0 46.450390 88.512793 315.288850
# 100 1.2 3 capsuleContent 7.5 50.329423 90.805099 360.118323
# 101 1.2 3 capsuleContent 8.0 54.208457 92.801847 406.020060
# 102 1.2 3 capsuleContent 8.0 54.208457 92.801847 406.020060
# 103 1.2 3 capsuleContent 9.0 60.439345 91.000989 497.921478
# 104 1.2 3 capsuleContent 10.0 66.670233 89.636393 588.240168
# 105 1.2 3 capsuleContent 12.0 76.322001 79.472871 757.349432
# 106 1.2 3 capsuleContent 16.0 87.256599 49.607701 1015.510577
# 107 1.2 3 capsuleContent 20.0 98.191197 40.968947 1196.663873
# 108 1.2 3 capsuleContent 24.0 104.177736 26.424419 1331.450604
I've fitted a regression tree. I've tried to save the fitted model with the save() function but if I close R, load the object and recall it I get a different output.
Here's the code:
training.set=iris[,-5]
library(tree)
set.seed(123)
part1 = sample(1:nrow(training.set), round(nrow(training.set)/2))
part2 = setdiff(1:nrow(training.set), part1)
tree.output = tree("Sepal.Length~.", data=training.set[part1,],
control=tree.control(nobs=length(part1), minsize=2, mindev=0.001))
prune.t = prune.tree(tree.output, newdata=training.set[part2,])
plot(prune.t)
J = prune.t$size[prune.t$dev == min(prune.t$dev)]
J
m.tree = prune.tree(tree.output,best=J)
save(m.tree,file="my model.RData")
m.tree
Output:
node), split, n, deviance, yval
* denotes terminal node
1) root 75 49.54000 5.756
2) Petal.Length < 4.3 41 6.69800 5.161
4) Petal.Length < 3.55 31 3.82800 5.019
8) Sepal.Width < 3.45 17 1.04900 4.794
16) Petal.Length < 1.45 9 0.52220 4.644 *
17) Petal.Length > 1.45 8 0.09875 4.962 *
9) Sepal.Width > 3.45 14 0.86930 5.293
18) Sepal.Width < 3.85 8 0.17880 5.138 *
19) Sepal.Width > 3.85 6 0.24000 5.500 *
5) Petal.Length > 3.55 10 0.32000 5.600 *
3) Petal.Length > 4.3 34 10.83000 6.474
6) Petal.Length < 5.7 28 3.46400 6.264
12) Petal.Width < 2.05 23 2.42600 6.187 *
13) Petal.Width > 2.05 5 0.26800 6.620 *
7) Petal.Length > 5.7 6 0.41500 7.450
14) Petal.Length < 6.35 4 0.02750 7.275 *
15) Petal.Length > 6.35 2 0.02000 7.800 *
Second part of the script:
q()
rm(list=ls() )
load("my model.RData")
m.tree
Output:
$frame
var n dev yval splits.cutleft splits.cutright
1 Petal.Length 75 49.5448000 5.756000 <4.3 >4.3
2 Petal.Length 41 6.6975610 5.160976 <3.55 >3.55
4 Sepal.Width 31 3.8283871 5.019355 <3.45 >3.45
8 Petal.Length 17 1.0494118 4.794118 <1.45 >1.45
16 <leaf> 9 0.5222222 4.644444
17 <leaf> 8 0.0987500 4.962500
9 Sepal.Width 14 0.8692857 5.292857 <3.85 >3.85
18 <leaf> 8 0.1787500 5.137500
19 <leaf> 6 0.2400000 5.500000
5 <leaf> 10 0.3200000 5.600000
3 Petal.Length 34 10.8261765 6.473529 <5.7 >5.7
6 Petal.Width 28 3.4642857 6.264286 <2.05 >2.05
12 <leaf> 23 2.4260870 6.186957
13 <leaf> 5 0.2680000 6.620000
7 Petal.Length 6 0.4150000 7.450000 <6.35 >6.35
14 <leaf> 4 0.0275000 7.275000
15 <leaf> 2 0.0200000 7.800000
$where
44 118 61 130 138 7 77 128 79 65 134 64 94 142 14 122 33 6 150 126 116
8 17 6 16 13 5 13 13 13 10 13 13 6 14 5 13 9 9 13 16 14
90 82 127 83 89 68 74 36 18 147 108 143 146 3 55 87 25 135 26 16 46
10 10 13 10 10 10 13 5 8 13 16 13 14 5 13 13 6 13 6 9 5
45 40 17 15 113 48 28 114 5 132 137 12 54 20 97 71 131 35 60 9 34
8 6 9 9 14 5 8 13 8 17 14 6 10 8 10 13 16 6 10 5 9
24 93 39 69 124 66 112 148 50 56 1 37
6 10 5 13 13 13 13 13 5 13 8 8
$terms
Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width
attr(,"variables")
list(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
attr(,"factors")
Sepal.Width Petal.Length Petal.Width
Sepal.Length 0 0 0
Sepal.Width 1 0 0
Petal.Length 0 1 0
Petal.Width 0 0 1
attr(,"term.labels")
[1] "Sepal.Width" "Petal.Length" "Petal.Width"
attr(,"order")
[1] 1 1 1
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
attr(,".Environment")
<environment: 0x00000000059480f0>
attr(,"predvars")
list(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
attr(,"dataClasses")
Sepal.Length Sepal.Width Petal.Length Petal.Width
"numeric" "numeric" "numeric" "numeric"
$call
snip.tree(tree = tree.output, nodes = c(19L, 18L, 5L, 16L, 13L,
12L))
$y
44 118 61 130 138 7 77 128 79 65 134 64 94 142 14 122 33 6 150 126 116
5.0 7.7 5.0 7.2 6.4 4.6 6.8 6.1 6.0 5.6 6.3 6.1 5.0 6.9 4.3 5.6 5.2 5.4 5.9 7.2 6.4
90 82 127 83 89 68 74 36 18 147 108 143 146 3 55 87 25 135 26 16 46
5.5 5.5 6.2 5.8 5.6 5.8 6.1 5.0 5.1 6.3 7.3 5.8 6.7 4.7 6.5 6.7 4.8 6.1 5.0 5.7 4.8
45 40 17 15 113 48 28 114 5 132 137 12 54 20 97 71 131 35 60 9 34
5.1 5.1 5.4 5.8 6.8 4.6 5.2 5.7 5.0 7.9 6.3 4.8 5.5 5.1 5.7 5.9 7.4 4.9 5.2 4.4 5.5
24 93 39 69 124 66 112 148 50 56 1 37
5.1 5.8 4.4 6.2 6.3 6.7 6.4 6.5 5.0 5.7 5.1 5.5
$weights
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[41] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
attr(,"class")
[1] "tree"
attr(,"xlevels")
attr(,"xlevels")$Sepal.Width
NULL
attr(,"xlevels")$Petal.Length
NULL
attr(,"xlevels")$Petal.Width
NULL
Do I need to use a different function to save the model?
Thank you.
Your object has been stored correctly, however, it is being printed out differently. The object m.tree is a list with class tree:
R> class(m.tree)
[1] "tree"
When you print the object, it looks for the function print.tree (vis the generic print function). This function is only available after you load the tree package.
library(tree)
load("my model.RData")
m.tree
will give you what you want.
If you are interested, run
getS3method("print", "tree")
to see the print method.