r函数,用于在满足特定条件时返回列号
我有一个如下所示的数据帧:r函数,用于在满足特定条件时返回列号,r,R,我有一个如下所示的数据帧: df<-data.frame(H0=c(35.4, NA, 36.0, 36.4), H1=c(32.3, 32.0, 34.3, 33.5), H2=c(33.4, 31.5, 33, 34.2), H3=c(32.9, 33.0, 34.0, 33.0), H4=c(32.8, NA, 34.5, 33.2)) 您的问题没有说明如何处理NAs或没有apply(df,1,function(x)match(TRUE
df<-data.frame(H0=c(35.4, NA, 36.0, 36.4), H1=c(32.3, 32.0, 34.3, 33.5),
H2=c(33.4, 31.5, 33, 34.2), H3=c(32.9, 33.0, 34.0, 33.0),
H4=c(32.8, NA, 34.5, 33.2))
您的问题没有说明如何处理
NA
s或没有<33的行max.col
对于您的任务来说可能已经足够好了:
R>df
H0 H1 H2 H3 H4
1 35.4 32.3 33.4 32.9 32.8
2 NA 32.0 31.5 33.0 NA
3 36.0 34.3 33.0 34.0 34.5
4 36.4 33.5 34.2 33.0 33.2
R>max.col(df <= 33, ties.method="first")
[1] 2 NA 3 4
您的问题没有说明如何处理
NA
s或没有<33的行max.col
对于您的任务来说可能已经足够好了:
R>df
H0 H1 H2 H3 H4
1 35.4 32.3 33.4 32.9 32.8
2 NA 32.0 31.5 33.0 NA
3 36.0 34.3 33.0 34.0 34.5
4 36.4 33.5 34.2 33.0 33.2
R>max.col(df <= 33, ties.method="first")
[1] 2 NA 3 4
您的问题没有说明如何处理
NA
s或没有<33的行max.col
对于您的任务来说可能已经足够好了:
R>df
H0 H1 H2 H3 H4
1 35.4 32.3 33.4 32.9 32.8
2 NA 32.0 31.5 33.0 NA
3 36.0 34.3 33.0 34.0 34.5
4 36.4 33.5 34.2 33.0 33.2
R>max.col(df <= 33, ties.method="first")
[1] 2 NA 3 4
您的问题没有说明如何处理
NA
s或没有<33的行max.col
对于您的任务来说可能已经足够好了:
R>df
H0 H1 H2 H3 H4
1 35.4 32.3 33.4 32.9 32.8
2 NA 32.0 31.5 33.0 NA
3 36.0 34.3 33.0 34.0 34.5
4 36.4 33.5 34.2 33.0 33.2
R>max.col(df <= 33, ties.method="first")
[1] 2 NA 3 4
如果要忽略NAs并在未找到值时放置NA
rowSearcher <- function(df) {
colNumbers <- numeric(0) # Vector of column numbers to output
for (r in 1:ncol(df)) { # Loop through the rows
for (c in 1:ncol(df)) { # Loop through the columns
if (!is.na(df[r, c]) && df[r, c] <= 33.0) {
colNumbers <- c(colNumbers, c)
break
}
if (c == ncol(df)) # Add an NA if no value was found
colNumbers <- c(colNumbers, NA)
}
}
return(colNumbers)
}
rowSearcher如果要忽略NAs并在未找到值时放置NA
rowSearcher <- function(df) {
colNumbers <- numeric(0) # Vector of column numbers to output
for (r in 1:ncol(df)) { # Loop through the rows
for (c in 1:ncol(df)) { # Loop through the columns
if (!is.na(df[r, c]) && df[r, c] <= 33.0) {
colNumbers <- c(colNumbers, c)
break
}
if (c == ncol(df)) # Add an NA if no value was found
colNumbers <- c(colNumbers, NA)
}
}
return(colNumbers)
}
rowSearcher如果要忽略NAs并在未找到值时放置NA
rowSearcher <- function(df) {
colNumbers <- numeric(0) # Vector of column numbers to output
for (r in 1:ncol(df)) { # Loop through the rows
for (c in 1:ncol(df)) { # Loop through the columns
if (!is.na(df[r, c]) && df[r, c] <= 33.0) {
colNumbers <- c(colNumbers, c)
break
}
if (c == ncol(df)) # Add an NA if no value was found
colNumbers <- c(colNumbers, NA)
}
}
return(colNumbers)
}
rowSearcher如果要忽略NAs并在未找到值时放置NA
rowSearcher <- function(df) {
colNumbers <- numeric(0) # Vector of column numbers to output
for (r in 1:ncol(df)) { # Loop through the rows
for (c in 1:ncol(df)) { # Loop through the columns
if (!is.na(df[r, c]) && df[r, c] <= 33.0) {
colNumbers <- c(colNumbers, c)
break
}
if (c == ncol(df)) # Add an NA if no value was found
colNumbers <- c(colNumbers, NA)
}
}
return(colNumbers)
}
rowSearcher您还可以使用:
apply(df,1,function(x) Position(function(y) y <=33 & !is.na(y), x))
#[1] 2 2 3 4
应用(df,1,函数(x)位置(函数(y)y您也可以使用:
apply(df,1,function(x) Position(function(y) y <=33 & !is.na(y), x))
#[1] 2 2 3 4
应用(df,1,函数(x)位置(函数(y)y您也可以使用:
apply(df,1,function(x) Position(function(y) y <=33 & !is.na(y), x))
#[1] 2 2 3 4
应用(df,1,函数(x)位置(函数(y)y您也可以使用:
apply(df,1,function(x) Position(function(y) y <=33 & !is.na(y), x))
#[1] 2 2 3 4
apply(df,1,函数(x)Position(函数(y)y您可以尝试match
,它返回第一次出现的索引
NA
被忽略,因为nomatch
的默认设置设置为NA\u integer\u
> apply(df, 1, function(x) match(TRUE, x <= 33.0))
# [1] 2 2 3 4
>apply(df,1,函数(x)match(TRUE,x您可以尝试match
,它返回第一次出现的索引
NA
被忽略,因为nomatch
的默认设置设置为NA\u integer\u
> apply(df, 1, function(x) match(TRUE, x <= 33.0))
# [1] 2 2 3 4
>apply(df,1,函数(x)match(TRUE,x您可以尝试match
,它返回第一次出现的索引
NA
被忽略,因为nomatch
的默认设置设置为NA\u integer\u
> apply(df, 1, function(x) match(TRUE, x <= 33.0))
# [1] 2 2 3 4
>apply(df,1,函数(x)match(TRUE,x您可以尝试match
,它返回第一次出现的索引
NA
被忽略,因为nomatch
的默认设置设置为NA\u integer\u
> apply(df, 1, function(x) match(TRUE, x <= 33.0))
# [1] 2 2 3 4
>apply(df,1,function(x)match(TRUE,x)请用您期望的答案更新您的原始帖子。请用您期望的答案更新您的原始帖子。请用您期望的答案更新您的原始帖子。(+1)对于我不知道存在的函数。(+1)对于我不知道存在的函数:(+1)对于我不知道存在的函数:(+1)对于我不知道存在的函数:)