99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀R語言字符串
R語言字符串
2017-06-16
收藏

R語言字符串

 使用一對(duì)單引號(hào)或雙引號(hào)在R語言中的任何值被視為字符串。在內(nèi)部R語言存儲(chǔ)的每串使用雙引號(hào)括起來,即使使用單引號(hào)創(chuàng)建。
在字符串中創(chuàng)建規(guī)則應(yīng)用
    在開始和結(jié)束字符串的引號(hào)應(yīng)該是兩個(gè)雙引號(hào)或兩個(gè)單引號(hào)。它們不能被混合。
    雙引號(hào)可以插入到一個(gè)字符串開始,以單引號(hào)結(jié)束。
    單引號(hào)可以插入一個(gè)字符串開始,以雙引號(hào)結(jié)束。
    雙引號(hào)不能插入到一個(gè)字符串的開始并以雙引號(hào)結(jié)束。
    單引號(hào)不能插入到一個(gè)字符串開始,以單引號(hào)結(jié)束。

有效的字符串示例
下面的例子闡明有關(guān)創(chuàng)建一個(gè)字符串在R語言中的規(guī)則
a <- 'Start and end with single quote'
print(a)

b <- "Start and end with double quotes"
print(b)

c <- "single quote ' in between double quotes"
print(c)

d <- 'Double quotes " in between single quote'
print(d)
當(dāng)上述代碼運(yùn)行時(shí),我們得到以下的輸出:
[1] "Start and end with single quote"
[1] "Start and end with double quotes"
[1] "single quote ' in between double quote"
[1] "Double quote \" in between single quote"
無效的字符串示例
e <- 'Mixed quotes"
print(e)

f <- 'Single quote ' inside single quote'
print(f)

g <- "Double quotes " inside double quotes"
print(g)

當(dāng)上述代碼運(yùn)行時(shí),我們得到以下的輸出:

...: unexpected INCOMPLETE_STRING

.... unexpected symbol
1: f <- 'Single quote ' inside

unexpected symbol
1: g <- "Double quotes " inside
字符串操作
連接字符串 - paste() 函數(shù)
R中許多字符串使用 paste() 函數(shù)來組合。它可以將任意數(shù)量的參數(shù)組合在一起。
語法
粘貼(paste)函數(shù)的基本語法是:
paste(..., sep = " ", collapse = NULL)
以下是所使用的參數(shù)的說明:
    ... - 表示要組合的任何數(shù)量的參數(shù)。
    sep - 表示參數(shù)之間的分隔符。它是任選的。
    collapse - 用于消除兩個(gè)字符串之間的空間。但不是在一個(gè)字符串的兩個(gè)詞的空間。
示例
a <- "Hello"
b <- 'How'
c <- "are you? "

print(paste(a,b,c))

print(paste(a,b,c, sep = "-"))

print(paste(a,b,c, sep = "", collapse = ""))
當(dāng)我們上面的代碼執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:
[1] "Hello How are you? "
[1] "Hello-How-are you? "
[1] "HelloHoware you? "
格式化數(shù)字和字符串 - format()函數(shù)
數(shù)字和字符串可以使用 format()函數(shù)的格式化為特定樣式。
語法
format()函數(shù)的基本語法是:
format(x, digits, nsmall,scientific,width,justify = c("left", "right", "centre", "none"))    
以下是所使用的參數(shù)的說明:
    x - 為向量輸入
    digits - 是顯示總位數(shù)
    nsmall - 是最小位數(shù)的小數(shù)點(diǎn)右邊
    scientific - 設(shè)置為TRUE,則顯示科學(xué)記數(shù)法
    width - 指示要通過填充空白在開始時(shí)顯示的最小寬度
    justify - 是字符串顯示在左邊,右邊或中心
示例
# Total number of digits displayed. Last digit rounded off.
result <- format(23.123456789, digits = 9)
print(result)

# Display numbers in scientific notation.
result <- format(c(6, 13.14521), scientific = TRUE)
print(result)

# The minimum number of digits to the right of the decimal point.
result <- format(23.47, nsmall = 5)
print(result)

# Format treats everything as a string.
result <- format(6)
print(result)

# Numbers are padded with blank in the beginning for width.
result <- format(13.7, width = 6)
print(result)

# Left justify strings.
result <- format("Hello",width = 8, justify = "l")
print(result)

# Justfy string with center.
result <- format("Hello",width = 8, justify = "c")
print(result)
當(dāng)我們上面的代碼執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:
[1] "23.1234568"
[1] "6.000000e+00" "1.314521e+01"
[1] "23.47000"
[1] "6"
[1] "  13.7"
[1] "Hello   "
[1] " Hello  "
統(tǒng)計(jì)字符串的字符數(shù) - ncahr()函數(shù)
函數(shù)計(jì)算字符數(shù)量,包括在一個(gè)字符串的空格的個(gè)數(shù)。
語法
nchar()函數(shù)的基本語法是:
nchar(x)
以下是所使用的參數(shù)的說明:
    x - 向量輸入。
示例
result <- nchar("Count the number of characters")
print(result)
當(dāng)我們上面的代碼執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:
[1] 30
改變大小寫 - toupper()和 tolower()函數(shù)
這些函數(shù)改變字符串的字符的大小寫。
語法
toupper()和 tolower()函數(shù)的基本語法為:
toupper(x)
tolower(x)
以下是所使用的參數(shù)的說明:
    x - 向量輸入。
示例
# Changing to Upper case.
result <- toupper("Changing To Upper")
print(result)

# Changing to lower case.
result <- tolower("Changing To Lower")
print(result)
當(dāng)我們上面的代碼執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:
[1] "CHANGING TO UPPER"
[1] "changing to lower"
提取字符串的一部分 - substring()函數(shù)
這個(gè)函數(shù)提取字符串的一部分。
語法
substring()函數(shù)的基本語法是:
substring(x,first,last)
以下是所使用的參數(shù)的說明:
    x - 是字符向量輸入。
    first - 是第一個(gè)字符要被提取的位置。
    last - 是最后一個(gè)字符要被提取的位置。
示例
# Extract characters from 5th to 7th position.
result <- substring("Extract", 5, 7)
print(result)
當(dāng)我們上面的代碼執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:
[1] "act"

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }