同求I'll give you hellele...

PunctuationPunctuation Matters
The following two letters contain the same exact words—but the punctuation differs sharply. Which letter would you rather receive?
Dear John:
I want a man who knows what love is all about. You are generous, kind, thoughtful. People who are not like you admit to being useless and inferior. You have ruined me for other men. I yearn for you. I have no feelings whatsoever when we're apart. I can forever be happy—will you let me be yours?
Dear John:
I want a man who knows what love is. All about you are generous, kind, thoughtful people, who are not like you. Admit to being useless and inferior. You have ruined me. For other men, I yearn. For you, I have no feelings whatsoever. When we're apart, I can forever be happy. Will you let me be?
HarrietThese letters tell us a lot about John and Harriet—but they tell us even more about the importance of punctuation. Change a comma here, move a period there … and the entire document is different. That's why it creates meaning as much as words do.
Show Me the Money
I'll bet that you know a lot more than you think about the correct way to use standard punctuation. To prove it, I'm going to ask you to take this simple quiz. In each case, write C if the punctuation is correct or W if the punctuation is incorrect. Answers and explanations follow.
Two men (1) ___ ,_ sentenced to die in the electric chair on the same day (2) ___ ,_ were led to the room in which they would meet their maker. The priest had given the last rites (3) ___ the warden had given the formal speech, and the participants had said a final prayer.
The warden, turning to the first man, solemnly asked (4) ___ “Son (5) ___ ,_ do you have a last request (6) ___ ?”
The man replied, “Yes sir, I do (7) ___ . I love dance music. Could you please play the Chicken Dance for me one last time?”
(8) ___ Certainly (9) ___ ,_”_ replied the Warden. He turned to the other man and asked, “Well (10) ___ ,_ what about you, son? What is your final request?”
“Please kill me first,” replied the other man.
C (use a comma to set off nonessential information)
C (use a comma to set off nonessential information)
W (comma needed to set off introductory clause)
W (comma needed before a direct quotation)
C (use a comma to set off a word of direct address)
C (use a question mark to place it inside the quotation marks)
C (use a period to indicate the end of a statement)
W (use quotation marks to set off dialogue)
C (use a comma to set off dialogue)
C (use a comma to set off interrupting expressions)
So how did you do?8 to 10 correct
Were you a proofreader in a past life?5 to 7 correct
There's no reason to hide under the bed when you see a semicolon.4 to 6 correct
I can help you, baby, I really can.1 to 3 correct
You love the Chicken Dance, you say?
Excerpted from The Complete Idiot's Guide to Grammar and Style © 2003 by Laurie E. Rozakis, Ph.D.. All rights reserved including the right
of reproduction in whole or in part in any form. Used by
arrangement with Alpha Books, a member of Penguin Group
(USA) Inc.To order this book direct from the publisher, visit the
or call 1-800-253-6476. You can also purchase this book atCalculate with vectors
This page explains how to get started doing basic calculations and using simple vector functions.
Introduction to vectors
A vector is a simple array of numbers or characters, such as the measurements of a single variable on a sample of individuals. R makes it easy to carry out mathematical operations and functions to all the values in a vector at once.
Enter measurements
Use the left arrow
“&-”
(“less than” sign followed by a dash) and the c function (for concatenate) to create a vector containing a set of measurements.
x &- c(11,42,-3,14,5)
# store these 5 numbers in vector x
x &- c(1:10)
# store integers 1 to 10
x &- c("Watson","Crick","Wilkins") # use quotes for character data
Use the seq function to generate a sequence of numbers and store in a vector,
x &- seq(0,10,by=0.1)
# 0, 0.1, 0.2, ... 9.9, 10
(note: “seq” results that include decimals may not be exact — the result &#” may not be exactly equal to the number 0.2 unless rounded using the “round” command)
Use rep to repeat values a specified number of times and store to a vector,
x &- rep(c(1,2,3),c(2,1,4))
# 1 1 2 3 3 3 3
To view contents of any object, including a vector, type its name and enter, or use the print command,
# print "x" to the screen
# do the same
Delete a vector
The following command removes the vector x from the local R environment.
Access elements of a vector
Use integers in square brackets to indicate specific elements of a vector. For example,
# 5th value of the vector x
# 2nd through 6th elements of x
x[2:length(x)] # everything but the first element
# everything but the first element
x[5] &- 4.2
# change the value of the 5th element to 4.2
Math with vectors
These operations are carried out on every element of the vector
# add 1 to each element of x
# square each element of x
# divide each element of x by 2
# multiply each element of x by 10
Operations on two vectors x and y work best when both are the same length (have the same number of elements). For example
# yields a new vector whose
# elements are x[1]*y[1], x[2]*y[2], ... x[n]*y[n]
If x and y are not the same length, then the shorter vector is elongated by starting again at the beginning.
Useful vector functions
Here is a selection of useful functions for data vectors. Many of the functions will also work on other data objects such as data frames, possibly with different effects.
Transform numerical data
The most common data transformations, illustrated using the single variable “x”.
# square root
sqrt(x+0.5)
# modified square root transformation
# the natural log of x
# log base 10 of x
# exponential ("antilog") of x
# absolute value of x
asin(sqrt(x))
# arcsine square root (used for proportions)
Statistics
Here are a few basic statistical functions on a numeric vector named x. Most of them will require the na.rm=TRUE option if the vector includes one or more missing values.
# the sum of values in x
# number of elements (including missing)
# sample mean
# sample variance
# sample standard deviation
# smallest element in x
# largest element in x
# smallest and largest elements in x
# median of elements in x
quantile(x)
# quantiles of x
# extracts only the unique values of x
# sort, smallest to largest
weighted.mean(x, w)
# weighted mean
What am I?
These functions return TRUE or FALSE depending on the structure of x and its data type.
is.vector(x)
is.character(x)
is.numeric(x)
is.integer(x)
is.factor(x)
Functions for character data
casefold(x)
# convert to lower case
casefold(x,upper=TRUE)
# convert to upper case
substr(x,2,4)
# extract 2nd to 4th characters of each element of x
paste(x,"ly",sep="")
# paste "ly" to the end of each element in x
# no. of characters in each element of x
grep("a",x)
# which elements of x contain letter "a" ?
strsplit(x,"a")
# split x into pieces wherever the letter "a" occurs
TRUE and FALSE (logical) data
Vectors can be assigned logical measurements, directly or as the result of a logical operation. Here’s an example of direct assignment.
z &- c(TRUE, TRUE, FALSE)
# put 3 logical values to a vector z
Logical operations can identify and select those vector elements for which a condition is TRUE. The logical operations are symbolized
(equal to)
(not equal to)
(less than)
(less than or equal to)
and so on.
For example, put the following numbers into a vector “z”,
z &- c(2, -1, 3, 99, 8 )
The following logical operations and functions yield the results shown on the right
# TRUE TRUE TRUE FALSE FALSE (for each element of z)
# FALSE FALSE TRUE TRUE TRUE
8, the elements of z for which the condition is TRUE
which(z &= 4)
# 4 5, the indices for elements of z satisfying the condition
is.vector(z)
is.character(z)
is.numeric(z)
# FALSE FALSE FALSE FALSE FALSE
any(z & 0)
all(z & 0)
The logical operators “&” and “|” refer to AND and OR. For example, put the following numbers into a vector “z”,
z &- c(-10, -5, -1, 0, 3, 92)
The following operations yield the results shown on the right
z & 0 & abs(z) & 5
# TRUE FALSE FALSE FALSE FALSE FALSE
z[z & 0 | abs(z) & 5]
Combine vectors to make a data frame
Multiple vectors representing different measurements (variables) made on the same individuals can be placed into columns of a data frame. A data frame is a spreadsheet-like object for a data set of potentially many variables. See the “Data” tab for tips on working with data frames. Here we show how to make a data frame by combining vectors of the same length. The vectors need not be of the same data type.
First, obtain your vectors. For example,
quadrat &- c(1:7)
site &- c(1,1,2,3,3,4,5)
species &- c("a","b","b","a","c","b","a")
Then combine them into a data frame named mydata.
mydata &- data.frame(quadrat = quadrat, site = site, species = species,
stringsAsFactors = FALSE)
The argument stringsAsFactors = FALSE is optional but recommended to preserve character data (otherwise character variables are converted to factors).
How to deal with missing values
Missing values in R are indicated with NA.
x[5] &- NA
# assign "missing" to the 5th element of x
x[x == -99] &- NA # change all instances of -99 in x to missing
which(is.na(x))
# identify which element(s) is missing
Some functions will treat NA as valid entries. For example, the length of a vector (number of elements) includes missing values in the count.
If you only want to include non-missing values, use this:
x1 <- na.omit(x)
# put the non-missing values of x into new vector x1
x1 <- x[complete.cases(x)] # same
x1 <- x[!is.na(x)])
length(x1)
# count the number of non-missing values
Some functions won't work on variables with missing values unless default options are modified. For example, if you try to calculate the mean of a vector that contains missing values you will get NA as your result. Most functions have an option "na.rm" that allows you to drop the missing values before you calculate.
x &- c(1,2,3,4,5,NA)
# a vector with one missing value
# result is NA
mean(x, na.rm = TRUE) # result is the mean of non-missing values of x
Write your own function
If R is missing a needed function write your own. Here's an example of a function named "sep" that calculates the standard error of an estimate of a proportion. The argument "n" refers to sample size, and "X" is the number of "successes" (e.g., the number of females in the sample, the number of infected individuals, etc.).
sep &- function(X, n){
p.hat &- X / n
# The proportion of "successes"
sep &- sqrt(p.hat*(1-p.hat)/(n-1))
# The standard error of p.hat
return(sep)
# Return the standard error as the result
To use the function, copy it to your clipboard. Then paste it into your command window and hit the enter key. (On a Mac, you may need to use the R Edit menu to "Paste as Plain Text" to avoid formatting problems.) The function "sep" will be stored in your R workspace so you only need to paste it once. If you save your workspace when you exit R it will remain there when you start up again -- otherwise you'll need to paste it in again.
To use the function on some data, for example n=20 and X=10, enter
sep(X = 10, n = 20) # yields the standard error
sep(10,20)
# shorthand ok if X and n are given in correct order
Write a loop to repeat a function
Loops are useful when you want to repeat a function or operation many times.
Here's a very simple loop that repeats the same command 5 times. The variable "i" is just a counter that starts at 1 and increases by 1 each time the commands between the brackets "{ }" are executed.
for(i in 1:5){
print("yes we can")
This next examples uses the counter to access a different element of a vector each time the loop is repeated. The following example prints the i'th element of the variable "x" on each iteration
x &- c(2,-1,3,99,8)
for(i in 1:length(x)){
print(x[i])
# use "print" to force printing inside loops
How to paste clipboard contents to a vector
To demonstrate, select the following 10 numbers with your mouse and copy to your clipboard:
76 75 -52 -70 52 8 -50 -6 57 5
(choose Edit -& Copy on your browser menu to copy to clipboard)
Then execute the following command in your R command window:
z &- scan("clipboard", what=numeric())
z &- scan(pipe("pbpaste"), what=numeric())
# on a Mac
To paste characters instead of numbers, use the following,
z &- scan("clipboard", what=character())
z &- scan(pipe("pbpaste"), what=character())
If characters or numbers of interest are separated by commas, use
z &- scan("clipboard", what=character(), sep=",")
z &- scan(pipe("pbpaste"), what=character(), sep=",")
Navigate this page

我要回帖

更多关于 give you 的文章

 

随机推荐