Wednesday, February 5, 2020

Vector and matrix with R

Vector and matrix with R

vector, nrow=number_of_rows


> url_to_open
[1] "http://finviz.com/export.ashx?v=152&c=0,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,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68"
> summary(finviz)
                                                                                         X..DOCTYPE.html.
 \t\t\t                <td><img src=/img/elite/no.png srcset=/img/elite/no_2x.png 2x alt=No></td>   : 24 
 \t\t                </tr>                                                                         : 18 
 \t\t                <tr>                                                                          : 18 
                 </div>                                                                          : 15 
                     </div>                                                                      : 12 
 \t\t\t                <td><img src=/img/elite/yes.png srcset=/img/elite/yes_2x.png 2x alt=Yes></td>:  8 
 (Other)                                                                                         :356 
> clean_numeric <- function(s){
+     s <- gsub("%|\\$|,|\\)|\\(", "", s)
+     s <- as.numeric(s)
+ }
> finviz <- cbind(finviz[,1:6],apply(finviz[,7:68], 2,
+                                    clean_numeric))
Error in `[.data.frame`(finviz, , 1:6) : undefined columns selected
> finviz <- cbind(finviz[,1:6],apply(finviz[,7:68], 2,
+                                    clean_numeric))
Error in `[.data.frame`(finviz, , 1:6) : undefined columns selected
> hist(finviz$Price, breaks=100, main="Price Distribution",
+      xlab="Price")
Error in hist.default(finviz$Price, breaks = 100, main = "Price Distribution",  :
  'x' must be numeric
> industry_avg_prices <-
+     aggregate(Price~Sector+Industry,data=finviz,FUN="mean")
Error in eval(predvars, data, env) : object 'Price' not found
> url <-
+     paste("http://sports.yahoo.com/nfl/stats/byteam?group=Offense&
+ cat=Total&conference=NFL&year=season_",year,"&sort=530&old_category=Total&old_group=Offense")
Error in paste("http://sports.yahoo.com/nfl/stats/byteam?group=Offense&\ncat=Total&conference=NFL&year=season_",  :
  object 'year' not found
> sector_avg <-
+     subset(sector_avg,variable%in%c("Price","P.E","PEG","P.S","P.B"))
Error in subset(sector_avg, variable %in% c("Price", "P.E", "PEG", "P.S",  :
  object 'sector_avg' not found
> a <- c(1, 2, 5, 3, 6, -2, 4)
> b <- c("one", "two", "three")
> c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)
> a <- c(1, 2, 5, 3, 6, -2, 4)
> a[3]
[1] 5
> a[c(1, 3, 5)]
[1] 1 5 6
> a[2:6]
[1]  2  5  3  6 -2
> myymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns,byrow=logical_value, dimnames=list(
+ char_vector_rownames, char_vector_colnames))
Error in as.vector(x, mode) :
  cannot coerce type 'closure' to vector of type 'any'
> y <- matrix(1:20, nrow=5, ncol=4)
> M <- matrix(1:20, nrow = 5, ncol = 4)
> y
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20
> x<-pretty(c(-5,5),30)
> k<-dnorm(x)
x<-pretty(c(-5,5),30) k<-dnorm(x)
Error: unexpected symbol in "x<-pretty(c(-5,5),30) k"
In addition: Warning messages:
1: In doTryCatch(return(expr), name, parentenv, handler) :
  "klab" is not a graphical parameter
2: In doTryCatch(return(expr), name, parentenv, handler) :
  "kaxs" is not a graphical parameter
3: In doTryCatch(return(expr), name, parentenv, handler) :
  "klab" is not a graphical parameter
4: In doTryCatch(return(expr), name, parentenv, handler) :
  "kaxs" is not a graphical parameter
> x<-pretty(c(-5,5),30)
>  y<-dnorm(x)
> plot(x,y,type ="1",xlab="Normal Daviate",ylab= "Density",yaxs='i')
Error in plot.xy(xy, type, ...) : invalid plot type '1'
> x <- pretty(c(-3,3), 30)
> y <- dnorm(x)
> plot(x, y,
+      type = "l",
+      xlab = "Normal Deviate",
+      ylab = "Density",
+      yaxs = "i"
+ )
> plot(x,y,type ="l",xlab="Normal Daviate",ylab= "Density",yaxs='i')
> pnorm(1.96)
[1] 0.9750021
> lm(mpg~wt, data=mtcars)

Call:
lm(formula = mpg ~ wt, data = mtcars)

Coefficients:
(Intercept)           wt
     37.285       -5.344

> lmfit <- lm(mpg~wt, data=mtcars)
> summary(lmfit)

Call:
lm(formula = mpg ~ wt, data = mtcars)

Residuals:
    Min      1Q  Median      3Q     Max
-4.5432 -2.3647 -0.1252  1.4096  6.8727

Coefficients:
            Estimate Std. Error t value Pr(>|t|) 
(Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
wt           -5.3445     0.5591  -9.559 1.29e-10 ***
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.046 on 30 degrees of freedom
Multiple R-squared:  0.7528, Adjusted R-squared:  0.7446
F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10

> plot(lmfit)
Hit <Return> to see next plot:
Hit <Return> to see next plot:
Hit <Return> to see next plot:
Hit <Return> to see next plot:
> cook<-cooks.distance(lmfit)
> plot(cook)
> predict(lmfit, mynewdata)
Error in predict.lm(lmfit, mynewdata) : object 'mynewdata' not found
> help(lm)
> library("vcd", lib.loc="~/R/win-library/3.6")
Loading required package: grid
> a <- c(1, 2, 5, 3, 6, -2, 4)
> b <- c("one", "two", "three")
> c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)
> a[3]
[1] 5
> a[c(1, 3, 5)]
[1] 1 5 6
> a[2:6]
[1]  2  5  3  6 -2
> myymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns,byrow=logical_value, dimnames=list(
+ char_vector_rownames, char_vector_colnames))
Error in as.vector(x, mode) :
  cannot coerce type 'closure' to vector of type 'any'
> y <- matrix(1:20, nrow=5, ncol=4)
> y
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20
> cells <- c(1,26,24,68)
> rnames <- c("R1", "R2")                                 cnames <- c("C1", "C2")
Error: unexpected symbol in "rnames <- c("R1", "R2")                                 cnames"
> rnames <- c("R1", "R2")
>  cnames <- c("C1", "C2")
> mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=TRUE,
+                    dimnames=list(rnames, cnames))
> mymatrix
   C1 C2
R1  1 26
R2 24 68
> mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=FALSE,
+                    dimnames=list(rnames, cnames))
> mymatrix
   C1 C2
R1  1 24
R2 26 68
>
> x <- matrix(1:10, nrow=2)
> x
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10
> x[2,]
[1]  2  4  6  8 10
> x[,2]
[1] 3 4
> x[,4]
[1] 7 8
> x[1,4]
[1] 7
> x[1, c(4,5)]
[1] 7 9
> myarray <- array(vector, dimensions, dimnames)
Error in as.vector(x, mode) :
  cannot coerce type 'closure' to vector of type 'any'
> dim1 <- c("A1", "A2")
>  dim2 <- c("B1", "B2", "B3")
>  dim3 <- c("C1", "C2", "C3", "C4")
> z <- array(1:24, c(2, 3, 4), dimnames=list(dim1, dim2, dim3))
> z
, , C1

   B1 B2 B3
A1  1  3  5
A2  2  4  6

, , C2

   B1 B2 B3
A1  7  9 11
A2  8 10 12

, , C3

   B1 B2 B3
A1 13 15 17
A2 14 16 18

, , C4

   B1 B2 B3
A1 19 21 23
A2 20 22 24

> patientID <- c(1, 2, 3, 4)
>  age <- c(25, 34, 28, 52)
>  diabetes <- c("Type1", "Type2", "Type1", "Type1")
>  status <- c("Poor", "Improved", "Excellent", "Poor")
> patientdata <-data.frame(patientID, age,diabetes,status)
> patientdata
  patientID age diabetes    status
1         1  25    Type1      Poor
2         2  34    Type2  Improved
3         3  28    Type1 Excellent
4         4  52    Type1      Poor
> patientdata[1,2]
[1] 25
> patientdata[1:2]
  patientID age
1         1  25
2         2  34
3         3  28
4         4  52
> patientdata[c("diabetes", "status")]
  diabetes    status
1    Type1      Poor
2    Type2  Improved
3    Type1 Excellent
4    Type1      Poor
> patientdata$age
[1] 25 34 28 52
> table(patientdata$diabetes, patientdata$status)
     
        Excellent Improved Poor
  Type1         1        0    2
  Type2         0        1    0
> attach(mtcars)
The following object is masked from package:ggplot2:

    mpg

> summary(mpg)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  10.40   15.43   19.20   20.09   22.80   33.90
> plot(mpg, disp)
> plot(mpg, wt)
> detach(mtcars)

No comments:

Post a Comment

Black-Scholes formula-R

 Black-Scholes formula-R > BlackScholes <- function(TypeFlag = c("c", "p"), S, X, Time, r, b, sigma) { TypeFla...