출처 : https://stat.ethz.ch/pipermail/r-help/2007-February/125860.html



[R] Randomly extract rows from a data frame

Using the 'iris' dataset in R: # Select 2 random rows > iris[sample(nrow(iris), 2), ] Sepal.Length Sepal.Width Petal.Length Petal.Width Species 96 5.7 3.0 4.2 1.2 versicolor 17 5.4 3.9 1.3 0.4 setosa # Select 5 random rows > iris[sample(nrow(iris), 5), ] Sepal.Length Sepal.Width Petal.Length Petal.Width Species 83 5.8 2.7 3.9 1.2 versicolor 12 4.8 3.4 1.6 0.2 setosa 63 6.0 2.2 4.0 1.0 versicolor 80 5.7 2.6 3.5 1.0 versicolor 49 5.3 3.7 1.5 0.2 setosa



Posted by Azel.Kim :

R studio 단축키

2016. 10. 9. 16:24 from 통계/R

출처 : https://support.rstudio.com/hc/en-us/articles/200711853-Keyboard-Shortcuts


Keyboard Shortcuts


This information is available directly in the RStudio IDE under the Tools menu:

Tools → Keyboard Shortcuts Help.


Console

DescriptionWindows & LinuxMac
Move cursor to ConsoleCtrl+2Ctrl+2
Clear consoleCtrl+LCtrl+L
Move cursor to beginning of lineHomeCommand+Left
Move cursor to end of lineEndCommand+Right
Navigate command historyUp/DownUp/Down
Popup command historyCtrl+UpCommand+Up
Interrupt currently executing commandEscEsc
Change working directoryCtrl+Shift+HCtrl+Shift+H
 

Source

DescriptionWindows & LinuxMac
Goto File/FunctionCtrl+.Ctrl+.
Move cursor to Source EditorCtrl+1Ctrl+1
New document (except on Chrome/Windows)Ctrl+Shift+NCommand+Shift+N
New document (Chrome only)Ctrl+Alt+Shift+NCommand+Shift+Alt+N
Open documentCtrl+OCommand+O
Save active documentCtrl+SCommand+S
Close active document (except on Chrome)Ctrl+WCommand+W
Close active document (Chrome only)Ctrl+Alt+WCommand+Option+W
Close all open documentsCtrl+Shift+WCommand+Shift+W
Preview HTML (Markdown and HTML)Ctrl+Shift+KCommand+Shift+K
Knit Document (knitr)Ctrl+Shift+KCommand+Shift+K
Compile NotebookCtrl+Shift+KCommand+Shift+K
Compile PDF (TeX and Sweave)Ctrl+Shift+KCommand+Shift+K
Insert chunk (Sweave and Knitr)Ctrl+Alt+ICommand+Option+I
Insert code sectionCtrl+Shift+RCommand+Shift+R
Run current line/selectionCtrl+EnterCommand+Enter
Run current line/selection (retain cursor position)Alt+EnterOption+Enter
Re-run previous regionCtrl+Shift+PCommand+Shift+P
Run current documentCtrl+Alt+RCommand+Option+R
Run from document beginning to current lineCtrl+Alt+BCommand+Option+B
Run from current line to document endCtrl+Alt+ECommand+Option+E
Run the current function definitionCtrl+Alt+FCommand+Option+F
Run the current code sectionCtrl+Alt+TCommand+Option+T
Run previous Sweave/Rmd codeCtrl+Alt+PCommand+Option+P
Run the current Sweave/Rmd chunkCtrl+Alt+CCommand+Option+C
Run the next Sweave/Rmd chunkCtrl+Alt+NCommand+Option+N
Source a fileCtrl+Shift+OCommand+Shift+O
Source the current documentCtrl+Shift+SCommand+Shift+S
Source the current document (with echo)Ctrl+Shift+EnterCommand+Shift+Enter
Fold SelectedAlt+LCmd+Option+L
Unfold SelectedShift+Alt+LCmd+Shift+Option+L
Fold AllAlt+OCmd+Option+O
Unfold AllShift+Alt+OCmd+Shift+Option+O
Go to lineShift+Alt+GCmd+Shift+Option+G
Jump toShift+Alt+JCmd+Shift+Option+J
Switch to tabCtrl+Shift+.Ctrl+Shift+.
Previous tabCtrl+F11Ctrl+F11
Next tabCtrl+F12Ctrl+F12
First tabCtrl+Shift+F11Ctrl+Shift+F11
Last tabCtrl+Shift+F12Ctrl+Shift+F12
Navigate backCtrl+F9Cmd+F9
Navigate forwardCtrl+F10Cmd+F10
Extract function from selectionCtrl+Alt+XCommand+Option+X
Extract variable from selectionCtrl+Alt+VCommand+Option+V
Reindent linesCtrl+ICommand+I
Comment/uncomment current line/selectionCtrl+Shift+CCommand+Shift+C
Reflow CommentCtrl+Shift+/Command+Shift+/
Reformat SelectionCtrl+Shift+ACommand+Shift+A
Show DiagnosticsCtrl+Shift+Alt+PCommand+Shift+Alt+P
Transpose Letters Ctrl+T
Move Lines Up/DownAlt+Up/DownOption+Up/Down
Copy Lines Up/DownShift+Alt+Up/DownCommand+Option+Up/Down
Jump to Matching Brace/ParenCtrl+PCtrl+P
Expand to Matching Brace/ParenCtrl+Shift+ECtrl+Shift+E
Select to Matching Brace/ParenCtrl+Shift+Alt+ECtrl+Shift+Alt+E
Add Cursor Above Current CursorCtrl+Alt+UpCtrl+Alt+Up
Add Cursor Below Current CursorCtrl+Alt+DownCtrl+Alt+Down
Move Active Cursor UpCtrl+Alt+Shift+UpCtrl+Alt+Shift+Up
Move Active Cursor DownCtrl+Alt+Shift+DownCtrl+Alt+Shift+Down
Find and ReplaceCtrl+FCommand+F
Find NextWin: F3, Linux: Ctrl+GCommand+G
Find PreviousWin: Shift+F3, Linux: Ctrl+Shift+GCommand+Shift+G
Use Selection for FindCtrl+F3Command+E
Replace and FindCtrl+Shift+JCommand+Shift+J
Find in FilesCtrl+Shift+FCommand+Shift+F
Check SpellingF7F7
 

Editing (Console and Source)

DescriptionWindows & LinuxMac
UndoCtrl+ZCommand+Z
RedoCtrl+Shift+ZCommand+Shift+Z
CutCtrl+XCommand+X
CopyCtrl+CCommand+C
PasteCtrl+VCommand+V
Select AllCtrl+ACommand+A
Jump to WordCtrl+Left/RightOption+Left/Right
Jump to Start/EndCtrl+Home/End or Ctrl+Up/DownCommand+Home/End or Command+Up/Down
Delete LineCtrl+DCommand+D
SelectShift+[Arrow]Shift+[Arrow]
Select WordCtrl+Shift+Left/RightOption+Shift+Left/Right
Select to Line StartAlt+Shift+LeftCommand+Shift+Left
Select to Line EndAlt+Shift+RightCommand+Shift+Right
Select Page Up/DownShift+PageUp/PageDownShift+PageUp/Down
Select to Start/EndCtrl+Shift+Home/End or Shift+Alt+Up/DownCommand+Shift+Up/Down
Delete Word LeftCtrl+BackspaceOption+Backspace or Ctrl+Option+Backspace
Delete Word Right Option+Delete
Delete to Line End Ctrl+K
Delete to Line Start Option+Backspace
IndentTab (at beginning of line)Tab (at beginning of line)
OutdentShift+TabShift+Tab
Yank line up to cursorCtrl+UCtrl+U
Yank line after cursorCtrl+KCtrl+K
Insert currently yanked textCtrl+YCtrl+Y
Insert assignment operatorAlt+-Option+-
Insert pipe operatorCtrl+Shift+MCmd+Shift+M
Show help for function at cursorF1F1
Show source code for function at cursorF2F2
Find usages for symbol at cursor (C++)Ctrl+Alt+UCmd+Option+U
 

Completions (Console and Source)

DescriptionWindows & LinuxMac
Attempt completionTab or Ctrl+SpaceTab or Command+Space
Navigate candidatesUp/DownUp/Down
Accept selected candidateEnter, Tab, or RightEnter, Tab, or Right
Dismiss completion popupEscEsc
 

Views

DescriptionWindows & LinuxMac
Move focus to Source EditorCtrl+1Ctrl+1
Move focus to ConsoleCtrl+2Ctrl+2
Move focus to HelpCtrl+3Ctrl+3
Show HistoryCtrl+4Ctrl+4
Show FilesCtrl+5Ctrl+5
Show PlotsCtrl+6Ctrl+6
Show PackagesCtrl+7Ctrl+7
Show EnvironmentCtrl+8Ctrl+8
Show Git/SVNCtrl+9Ctrl+9
Show BuildCtrl+0Ctrl+0
Sync Editor & PDF PreviewCtrl+F8Cmd+F8
Show Keyboard Shortcut ReferenceAlt+Shift+KOption+Shift+K
 

Build

DescriptionWindows & LinuxMac
Build and ReloadCtrl+Shift+BCmd+Shift+B
Load All (devtools)Ctrl+Shift+LCmd+Shift+L
Test Package (Desktop)Ctrl+Shift+TCmd+Shift+T
Test Package (Web)Ctrl+Alt+F7Cmd+Alt+F7
Check PackageCtrl+Shift+ECmd+Shift+E
Document PackageCtrl+Shift+DCmd+Shift+D
 

Debug

DescriptionWindows & LinuxMac
Toggle BreakpointShift+F9Shift+F9
Execute Next LineF10F10
Step Into FunctionShift+F4Shift+F4
Finish Function/LoopShift+F6Shift+F6
ContinueShift+F5Shift+F5
Stop DebuggingShift+F8Shift+F8
 

Plots

DescriptionWindows & LinuxMac
Previous plotCtrl+Alt+F11Command+Option+F11
Next plotCtrl+Alt+F12Command+Option+F12
 

Git/SVN

DescriptionWindows & LinuxMac
Diff active source documentCtrl+Alt+DCtrl+Option+D
Commit changesCtrl+Alt+MCtrl+Option+M
Scroll diff viewCtrl+Up/DownCtrl+Up/Down
Stage/Unstage (Git)SpacebarSpacebar
Stage/Unstage and move to next (Git)EnterEnter
 

Session

DescriptionWindows & LinuxMac
Quit Session (desktop only)Ctrl+QCommand+Q
Restart R SessionCtrl+Shift+F10Command+Shift+F10


Posted by Azel.Kim :

1) 우선 cluster 패키지를 불러와서 라이브러리 선언


install.packages("cluster")

library(cluster)


2) 거리 데이터를 행렬로 입력


d<-matrix(c(

  0, 1, 11, 5,

  1, 0, 2, 3,

  11, 2, 0, 4,

  5, 3, 4, 0), nrow=4, ncol=4, byrow=T)


3) 거리 데이터를 입력한 행렬을 거리행렬로 지정하기 위해서 as.dist() 옵션을 사용


d<-as.dist(d)


4) 계보적 군집분석을 위한 hclust(d, method="방법") 함수를 사용하려면 거리행렬(d)만 사용가능


hclust(d, method="single") # single linkage

hclust(d, method="complete") # complete linkage

hclust(d, method="average") # average linkage


5) dendrogram으로 보려면 plot()함수 사용


plot(hclust(d, method="single"))

plot(hclust(d, method="complete"))

plot(hclust(d, method="average"))

Posted by Azel.Kim :

SAS gplot

2014. 11. 5. 20:50 from 통계/SAS

http://blog.naver.com/wjddudwo209/220125459139


2. 그림 그리기

 

  1) proc gplot



PROC GPLOT DATA= 데이터셋이름 ;​

PLOT X*Y=C ;

run; quit;​

(C에는 숫자를 넣어준다. 1은 별표, 2는 동그라미 3은 .. 이런식으로 숫자를 넣어 점의 모양을 정한다.) ​



 



 

  /option(s)

LEGEND=LEGENDn : 산점도와 함께 출력될 범례 지정
OVERLAY : 여러개 그림을 겹쳐 그림
HREF or VREF = values : 수직(수평)참조선
LH(LHREF) LV(LVREF) = line type 수직(수평)보조선을 그리는 선종류 지정
HAXIS or VAXIS = a to b by c : 수평(수직)축 기준점 지정(축의 tick을 a에서 b까지 c의
간격으로 그림)
= AXISn n번째의 AXIS로 정의된 축의 모양을 사용한다.
NOAXIS(NOAXES): 좌표축을 그리지 않음
NOLEGEND : 범례 없앰
FRAME : plot을 상자 안에 그림
- SAS/GRAPH에서 사용되는 옵션들 중 SYMBOL 문
: GPLOT 절차에서 기호, 선, 색상 및 평활방법들을 정의하는 데 사용
SYMBOLn options;
options
SAS command 설명
I = options
(보간법)
NONE 산점도

 

JOIN 직선으로 연결
NEEDLE 수평축과 점들을 바늘 모양으로 연결
SPLINE spline법
V = symbol | NONE 관측값을 나타낼 기호를 지정 (0-9 , A-Z , 특수부호의 이름)
C = symbol-color 점과 연결선의 색 지정
CI = line-color 연결선의 색 지정
CV = value-color 점의 색 지정
H = height 출력 기호의 크기 (단위 pct, cm, in)
L = line-type 연결선의 종류지정(1에서 46까지 가능, 1: 실선, 2: 점선)
WIDTH = width 연결선의 굵기 지정 (default=1)


  2) proc sgplot  (9.1 버전에서는 안됨)


gplot 과 비슷한 것.


PROC SGPLOT DATA= 데이터셋이름;

SCATTER X=variable Y=variable </options>;
XAXIS <options>;
YAXIS <options>;
RUN;

  /option(s) 

/markerattrs = (color =기호);

이 기호에 알맞은 문자를 넣음으로 점의 색을 바꿀 수 있다.

 

 



 

 

 

색깔에 관련된 기호! Abbreviation에 해당하는 것을 기호 에 넣으면 된다.

 

/markerattrs = (color =기호);

 

또 다른 예제 코드)


PROC SGPLOT DATA=table_intro_1;
TITLE1 BOLD "TableI_1" /* 제목 */
SCATTER x=gdp y=pce;
XAXIS LABEL="GDP" ;
YAXIS LABEL="PCE";
FOOTNOTE1 JUSTIFY=LEFT 'date: 1960 ~ 2005';
RUN;


'통계 > SAS' 카테고리의 다른 글

SAS goptions  (0) 2014.11.05
SAS color chart  (0) 2014.11.05
Posted by Azel.Kim :

SAS goptions

2014. 11. 5. 20:48 from 통계/SAS

VALUE=special-symbol | text-string | SPECIAL | NONE

specifies a plot symbol for the data points (GPLOT and GBARLINE).

  • If you omit the SYMBOL statement, plot points are generated using the default plot symbols. If you specify a SYMBOL statement, but do not specify the VALUE= option, the plot symbols are suppressed.
  • specifies contour-label text in a contour plot (GCONTOUR). By default with the AUTOLABEL option, GCONTOUR labels contour lines with the contour variable's value at that contour level.
  • VALUE=SPECIAL enables you to define unique special symbols for up to 12 plots (GPLOT) in one SYMBOL statement. Some of the symbols include the following: CIRCLE, DIAMOND, DIAMONDFILLED, DOT, HASH, PAW, POINT, PLUS, SQUARE, SQUAREFILLED, STAR, TRIANGLE, TRIANGLEFILLED, X, Y, and Z. See Special Symbols for Plotting Data Points for the complete list. This option is useful when the number of plots in your graph is variable. Rather than writing a SYMBOL statement for each possible plot, you can use VALUE=SPECIAL in one SYMBOL statement to define symbols for up to 12 individual plots.
  • VALUE=NONE suppresses plot symbols at the data points, or labels on the contour lines. You can set the VALUE=NONE option independent of the INTERPOL= option.
Values for special-symbol are the names and characters shown in Special Symbols for Plotting Data Points. The special symbol table can be used only if the FONT= option is not used or a null value is specified:
font=,
This means that you cannot use VALUE=special-symbol or VALUE=SPECIAL if you are using the FONT= option.
Special Symbols for Plotting Data Points
Special Symbols for Plotting Data Points
Note: The words or special characters in the VALUE= column are entered exactly as shown.

To specify a single quotation mark, you must enclose it in double quotation marks:

value="'"

To specify a double quotation mark, you must enclose it in single quotation marks:

value='"'

In some operating environments, punctuation characters might require single quotation marks.

If you use VALUE=text-string to specify a plot symbol, you must also use the FONT= option to specify a symbol font or a text font. If you specify a symbol font, the characters in the string are character codes for the symbols in the font. If you specify a text font, the characters in the string are displayed. If you specify a text string containing quotation marks or blanks, enclose the string in single quotation marks.

For example, if you specify this statement, the plot symbol is the word "plus" instead of the symbol +:

symbol font=swiss value=plus;

Java and ActiveX support the following characters from the Marker Font for special-symbol and SPECIAL:

Marker-font Symbols Supported by Java and ActiveX
Character
Aliases
Marker
Cone, Pyramid, Default
Square
Cube
Star
Circle
Sphere, Dot, Balloon
Plus
Cross
Flag
Y
X
Prism
Z
Spade
Heart
#
Diamond
$
Club
%
Hexagon
Paw
Cylinder
Hash
Note: If you do not use a SYMBOL statement to specify a color for each symbol, but you do specify a color list in a GOPTIONS statement, then Java and ActiveX assign colors to symbols differently than do the other device drivers. To ensure consistency on all devices, for VALUE=special-symbol or VALUE=text-string, you should specify the desired color of each symbol. If you do not specify a symbol color, SAS/GRAPH uses the first default color and the first symbol. It uses each color in the list of default colors until the list is exhausted. SAS/GRAPH then selects the next symbol and begins again with the first default color. It rotates the new symbol through the list of default colors before selecting another symbol. It continues selecting new symbols and colors until no more symbols are needed.

For VALUE=SPECIAL, the way in which symbols and their colors are assigned is different from VALUE=special-symbol and VALUE=text-string. For VALUE=SPECIAL, when generating symbols for a plot, rather than rotating each symbol through the list of colors, SAS/GRAPH rotates through the 12 special symbols and assigns each symbol a color from the color list. The symbols and the symbol colors that are used in a plot depend on the following:

  • whether a symbol color is specified in the SYMBOL statement or in the GOPTIONS statement
  • the number of colors in the color list when a symbol color is not specified
AliasV=
RestrictionPartially supported by Java and ActiveX
NotesFor ActiveX output, the VALUE= option is not supported when INTERPOL=HILO or INTERPOL=STD. You can use the OVERLAY option with GPLOT to get symbols to appear on the data points.
The VALUE option overrides the MarkerSymbol attribute in graph styles.
SeeSymbol Sequences Generated from SYMBOL Statements
FONT=font | NONE
Specifying Plot Symbols
About Special Fonts and Symbols in JAVA for a description of the special symbols that the Java devices support and the value that you must specify in the SYMBOL statement VALUE= option for each.
About Special Fonts and Symbols in ACTIVEX for a description of the special symbols that the ActiveX devices support and the value that you must specify in the SYMBOL statement VALUE= option for each.
Rotating Plot Symbols through the Color List

Creating and Modifying Box Plots

Labeling Contour Lines, Modifying the Horizontal Axis, Modifying the Legend

WIDTH=thickness-factor

specifies the thickness of interpolated lines (GPLOT) or contour lines (GCONTOUR). thickness-factor is a number. The thickness of the line increases directly with thickness-factor. By default, WIDTH=1.

WIDTH= also affects all the lines in box plots (INTERPOL=BOX), high-low plots with bars (INTERPOL=HILOB), and standard deviation plots (INTERPOL=STD). It also affects the outlines of the area generated by the AREAS= option in the PLOT statement of the GPLOT procedure.

AliasW=
Style reference LineThickness attribute of the GraphAxisLines element
RestrictionPartially supported by Java and ActiveX
NotesBy default, the value specified by WIDTH= is used as the default value for the BWIDTH= option. For example, specifying WIDTH=6 also sets BWIDTH= to 6 unless you explicitly assign a value to the BWIDTH= option.
Java and ActiveX do not provide the same measure of control for width as the other SAS/GRAPH device drivers. Measurements are translated to pixels rather than a percentage. For the JAVA, JAVAIMG, ACTIVEX, and ACTXIMG devices, the maximum width is 6.
Ordering Axis Tick Marks with SAS Date Values

Creating and Modifying Box Plots


'통계 > SAS' 카테고리의 다른 글

SAS gplot  (0) 2014.11.05
SAS color chart  (0) 2014.11.05
Posted by Azel.Kim :