简体   繁体   English

如何确定等于 r 中某个值的列的长度?

[英]How do I determine the length of a column equal to a certain value in r?

I'm trying to find how many data points are present for each of my categorical variables in the column genotype.我试图找出列基因型中每个分类变量存在多少数据点。 So far the following code returns the same values when the first line should return a value roughly 1/3 of the lower line of code.到目前为止,当第一行应该返回大约是下一行代码的 1/3 时,以下代码返回相同的值。

length(CYP$Genotype == "CYP1B1 KO")
length(CYP$Genotype)

As mentioned in the comments, you want to use sum instead of length to get the frequency of a variable.如评论中所述,您想使用sum而不是length来获取变量的频率。 If you use length on the condition, then it will return the number of items in the vector, which is 8 in this case.如果在条件上使用length ,那么它将返回向量中的项目数,在这种情况下为 8。

CYP$Genotype == "CYP1B1 KO"
#[1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE

length(CYP$Genotype == "CYP1B1 KO")
# [1] 8

Instead, if you use sum , then it will count the number of TRUE statements (which are counted as 1s, whereas, FALSE is a 0).相反,如果您使用sum ,那么它将计算TRUE语句的数量(计为 1,而FALSE为 0)。

sum(CYP$Genotype == "CYP1B1 KO")
# [1] 4

As mentioned by @dcarlson, you can use table to get the frequency of the different values in the column, which you could put back into a dataframe.正如@dcarlson 所提到的,您可以使用table来获取列中不同值的频率,您可以将其放回数据框中。

data.frame(n = cbind(table(CYP$Genotype)))
#          n
#CYP1B1 KO 4
#GRB3C2 F2 1
#RGB2B1 G1 3

Or you can use count from dplyr :或者您可以使用dplyr中的count

library(dplyr)

CYP %>% 
  count(Genotype)

#   Genotype n
#1 CYP1B1 KO 4
#2 GRB3C2 F2 1
#3 RGB2B1 G1 3

Data数据

CYP <- structure(list(Genotype = c("CYP1B1 KO", "CYP1B1 KO", "CYP1B1 KO", 
"CYP1B1 KO", "RGB2B1 G1", "RGB2B1 G1", "RGB2B1 G1", "GRB3C2 F2"
)), class = "data.frame", row.names = c(NA, -8L))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用R中较大的数据帧使用大于#的等长行创建新的数据帧? - How Do I Create a New Data Frame Using Rows of Equal Length Above a Certain # From Larger Data Frame in R? 如何在一列中将值分成相等的范围,并在R中将另一列的关联值求和? - How do I split values into equal ranges in one column and sum the associated value of another column in R? 如何在一定长度下转换 r 中的列? - How can I transform a column in r under a certain length? 在 R 中:如何根据相邻列中某个值的倍数增加列值 - In R: How do I increment a column value based on multiples of a certain value in the adjacent column 在R中,如何提取最接近某个值的数组中某个行的列号? - In R, how do I extract the column number of a certain row in an array that's closest to a certain value? 如果另一列中的某个值是异常值,如何在 R 中创建一个新列,该列为 1? - How do I create a new column in R that is 1 if a certain value in another column is an outlier? 如何根据列名的长度对 R 中的列进行子集化? - How do I subset columns in R based on the length of the column names? 在 R 中,如何相对于另一个等长向量的元素有条件地填充向量的元素? - In R, how do I fill elements of a vector conditionally with respect to elements of another vector of equal length? 如何引用 r 中的列中的值? - How do I refer to a value in a column in r? 如何将某些行移动到 R 中的列? - How do I move certain rows to column in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM