||
1. 创建新变量
mydata <- transform(mydata,
sumx = x1 + x2,
meanx = (x1 + x2)/2)
2. 变量重编码
(1)within函数
leadership <- within(leadership, {
agecat < NA -----新建一个变量
agecat[ age > 75 ] <- "Elder"
agecat[ age > = 55 & age <= 75 ] <- "Middle Aged" -----根据Age的值判断
agecat[ age < 55 ] <- "Young" })
(2)reshape函数
3. Mac笔记本读取含有中文字符的文件
read.csv("filename", fileEncoding = "GBK")可以避免编码错误
4. factor类型转成numeric类型之前,一定要先转成character类型,即as.numeric(as.character()),不然数会变
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2025-1-5 14:20
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社