标签:r语言
字符串和因子> c("HELLO","WORLD")[1] "HELLO" "WORLD"
> paste(c("hello","hi"),"world")[1] "hello world" "hi world"
> paste(c("hello","hi"),"world",sep="-")[1] "hello-world" "hi-world"> paste(c("hello","hi"),"world",collapse="!")[1] "hello world!hi world"> paste0(c("hello","hi"),"world") #paste0函数能去掉分隔符[1] "helloworld" "hiworld"
> x[1] 1 4 9 16 25> toString(x)[1] "1, 4, 9, 16, 25"> toString(x,width=2) #width表示限制字符长度[1] "1,...."
> toupper("hello world!")[1] "HELLO WORLD!"> tolower("HELLO WORLD!")[1] "hello world!"
> pow <- 1:3> (power_of_e <- exp(pow))[1] 2.718282 7.389056 20.085537> formatC(power_of_e)[1] "2.718" "7.389" "20.09"> formatC(power_of_e,digits=3) #保留三个数字[1] "2.72" "7.39" "20.1"> formatC(power_of_e,digits=3,width=10) #在前面添加空格[1] " 2.72" " 7.39" " 20.1"> formatC(power_of_e,digits=3,format="e") #科学计数法[1] "2.718e+00" "7.389e+00" "2.009e+01"> formatC(power_of_e,digits=3,flag="+") +前面加上+[1] "+2.72" "+7.39" "+20.1"
> sprintf("%s %d %e", "Euler‘s constant to the power",pow,power_of_e)[1] "Euler‘s constant to the power 1 2.718282e+00"[2] "Euler‘s constant to the power 2 7.389056e+00"[3] "Euler‘s constant to the power 3 2.008554e+01"
> (heights <- data.frame(height_cm = c(153,181,150,172,165),gender = c("female","male","male","female","male")))height_cm gender1 153 female2 181 male3 150 male4 172 female5 165 male> class(heights$gender)[1] "factor"> heights$gender[1] female male male female maleLevels: female male #female和male称为因子水平
> gender_char <- c("female","male","male","female","male")> (gender_factor <- factor(gender_char))[1] female male male female maleLevels: female male
R语言学习(5)-字符串和因子,布布扣,bubuko.com
标签:r语言
原文地址:http://blog.csdn.net/luoyhang003/article/details/38349401