一些R命令(3)

> (2+4i)^-3.5+(2i+4.5)*(-1.7-2.3i)/((2.6-7i)*(-4+5.1i))
[1] -0.2790593-0.091246i
> (z <- complex(real=rnorm(10),imaginary = rnorm(10)))
 [1] -0.7553994+1.8698082i
 [2] -0.5319452-2.2838667i
 [3] -0.0364400-0.5610348i
 [4] -1.2155822-0.6701850i
 [5]  1.9848440+1.8560497i
 [6]  0.2666800-1.3724982i
 [7] -2.5660278+0.6403266i
 [8]  0.2124165+0.7804393i
 [9] -0.8820738+0.9604197i
[10] -0.1141126+0.7639536i
> Re(z)
 [1] -0.75539938 -0.53194517 -0.03643998
 [4] -1.21558222  1.98484404  0.26667999
 [7] -2.56602775  0.21241649 -0.88207379
[10] -0.11411258
> Im(z)
 [1]  1.8698082 -2.2838667 -0.5610348
 [4] -0.6701850  1.8560497 -1.3724982
 [7]  0.6403266  0.7804393  0.9604197
[10]  0.7639536
> Mod(Z)
Error: object 'Z' not found
> Mod(z)
 [1] 2.0166335 2.3449974 0.5622170
 [4] 1.3880879 2.7174485 1.3981665
 [7] 2.6447148 0.8088301 1.3040170
[10] 0.7724292
> Arg(z)
 [1]  1.9547448 -1.7996308 -1.6356566
 [4] -2.6377302  0.7518783 -1.3788850
 [7]  2.8970470  1.3050579  2.3136984
[10]  1.7190711
> choose(3,2)
[1] 3
> factorial(6)
[1] 720
> f=function(x) x^3-2*x-1
> uniroot(f,c(0,2))
$`root`
[1] 1.618018

$f.root
[1] -9.17404e-05

$iter
[1] 6

$init.it
[1] NA

$estim.prec
[1] 6.103516e-05

> f
function(x) x^3-2*x-1
<bytecode: 0x0000000005b3c718>
> f=function(x) x^2+2*x+1
> optimize(f,c(-2,2))
$`minimum`
[1] -1

$objective
[1] 0

> a=factor(letters[1:10])
> a
 [1] a b c d e f g h i j
Levels: a b c d e f g h i j
> a[3]
[1] c
Levels: a b c d e f g h i j
> a[3]='w'
Warning message:
In `[<-.factor`(`*tmp*`, 3, value = "w") :
  invalid factor level, NA generated
> a[3]='w'
Warning message:
In `[<-.factor`(`*tmp*`, 3, value = "w") :
  invalid factor level, NA generated
> a
 [1] a    b    <NA> d    e    f    g   
 [8] h    i    j   
Levels: a b c d e f g h i j
> a=as.character(a)
> a
 [1] "a" "b" NA  "d" "e" "f" "g" "h" "i"
[10] "j"
> a[3]='w'
> a
 [1] "a" "b" "w" "d" "e" "f" "g" "h" "i"
[10] "j"
> a;
 [1] "a" "b" "w" "d" "e" "f" "g" "h" "i"
[10] "j"
> a;factor(a)
 [1] "a" "b" "w" "d" "e" "f" "g" "h" "i"
[10] "j"
 [1] a b w d e f g h i j
Levels: a b d e f g h i j w
> x=scan()
1: 1.5 2.6 3.7 2.1 8.9 12 -1.2 -4
9: 
Read 8 items
> x
[1]  1.5  2.6  3.7  2.1  8.9 12.0 -1.2 -4.0
> x=c(1.5,2.6,3.7,2.1,8.9,12,-1.2,-4)
> w=read.table(file.choose(),header=T)
Error in file.choose() : file choice cancelled
> x
[1]  1.5  2.6  3.7  2.1  8.9 12.0 -1.2 -4.0
> write(x,"test.txt")
> y=scan("test.txt");
Read 8 items
> y
[1]  1.5  2.6  3.7  2.1  8.9 12.0 -1.2 -4.0
> y=iris;
> y
> y[1:5,]
  Sepal.Length Sepal.Width Petal.Length
1          5.1         3.5          1.4
2          4.9         3.0          1.4
3          4.7         3.2          1.3
4          4.6         3.1          1.5
5          5.0         3.6          1.4
  Petal.Width Species
1         0.2  setosa
2         0.2  setosa
3         0.2  setosa
4         0.2  setosa
5         0.2  setosa
> str(y)
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
> write.table(y,"test.txt",row.names=F)
> w=read.table("test.txt",header=T)
> w
> nrow(w)
[1] 150
> ncol(w)
[1] 5
> str(w)#汇总
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
> str(w)
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
> data=read.table
> data=read.table("clipboard")
Warning message:
In read.table("clipboard") :
  incomplete final line found by readTableHeader on 'clipboard'
> data
   V1  V2  V3  V4     V5
1 4.8 3.4 1.6 0.2 setosa
> data=read.table("clipboard",header = F)
Warning message:
In read.table("clipboard", header = F) :
  incomplete final line found by readTableHeader on 'clipboard'
> data
   V1  V2  V3  V4     V5
1 4.8 3.4 1.6 0.2 setosa
> data=read.table("clipboard")
Warning message:
In read.table("clipboard") :
  incomplete final line found by readTableHeader on 'clipboard'
> data
            V1          V2           V3          V4
1 Sepal.Length Sepal.Width Petal.Length Petal.Width
2          5.1         3.5          1.4         0.2
       V5
1 Species
2  setosa
> data=read.table(,header = T)
Error in read.table(, header = T) : 
  argument "file" is missing, with no default
> data
            V1          V2           V3          V4
1 Sepal.Length Sepal.Width Petal.Length Petal.Width
2          5.1         3.5          1.4         0.2
       V5
1 Species
2  setosa

猜你喜欢

转载自blog.csdn.net/feynman1999/article/details/81136295