我在根据数据集中的另一列将数据集中的一列划分为三元组时遇到了问题。例如,如何根据基因表达水平将基因表达水平分为三组(低、中、高)?数据集中的列具有
基因
作为一列,
表达式
作为另一列。
我正在考虑使用这个函数:
排序(数据集名称$表达式)
因此,这将从最高到最低对表达式级别进行排序。但是,我不确定如何标记哪些是低、中或高,以及如何为每一个创建新的子集?
提前感谢!
发布于 2021-02-28 07:28:14
这是一个使用R附带的虹膜示例数据集的示例。在这里,tertiles将基于可变的花瓣长度。
# generate tertile limits using the quantile function,
# with proportion spacing of 0 to 1 at .33 intervals.
# These 4 values represent the start and end points in terms of Petal Length,
# of the three terriles.
tertile_limits <- quantile(iris$Petal.Length, seq(0, 1, 1/3), na.rm = TRUE)
# use the tertile start and end points (4 points, which creates 3 intervals)
# to create a new factor in the dataset
# The three tertiles are also explicitly labelled Low, Medium, and High, though this is optional.
iris$Petal.Length.Tertiles <- cut(iris$Petal.Length, tertile_limits, c('Low', 'Medium', 'High'), include.lowest = TRUE)发布于 2021-02-28 07:35:35
您可以使用
函数,然后使用
函数。下面是一个使用mtcar和mpg的示例:
cars <- mtcars
breaks <- quantile(cars$mpg, c(.33, .67, 1))
breaks <- c(0, breaks)
labels <- c('low', 'medium', 'high')
cuts <- cut(cars$mpg, breaks = breaks, labels = labels)
cars <- cbind(cars, cuts)
head(cars)
mpg cyl disp hp drat wt qsec vs am gear carb cuts
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 medium
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 medium
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 high
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 medium
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 medium
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 mediumhttps://stackoverflow.com/questions/66404334
复制相似问题