1. Creating new variable for existing data frame

mydata<-data.frame(x1 = c(2, 2, 6, 4), x2 = c(3, 4, 2, 8))
1.mydata$sumx <- mydata$x1 + mydata$x2
mydata$meanx <- (mydata$x1 + mydata$x2)/2
2.attach(mydata) mydata$sumx <- x1 + x2
mydata$meanx <- (x1 + x2)/2
detach(mydata)
3.mydata <- transform(mydata, sumx = x1 + x2, meanx = (x1 + x2)/2)
2.Recoding Variables
e.g. recode the age into categorical variable agecat(Young, Middle aged, Elder)

change variable name
1. fix()
2.
Missing Value




Sorting data
order()
the sorting order is normally ascending.
Merging datasets

本文介绍R语言中数据操作的基本技巧,包括为现有数据框创建新变量、重新编码变量及更改变量名等。此外,还涉及处理缺失值和数据排序的方法,并展示了如何合并数据集。
2109

被折叠的 条评论
为什么被折叠?



