当我到达R中的最后一行数据时,如何获得逻辑(T/F)结果?

发布于 2025-01-25 04:34:31 字数 563 浏览 1 评论 0原文

我希望根据我是否达到了最后一行的数据来获取T/F值的向量。我在下面附上了一个示例数据框架。

df <- structure(list(A = 1:24, B = 2:25), class = "data.frame", row.names = c(NA, -24L))

这是我所需的输出给定提供的数据。

c("F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", 
  "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "T")

如果这是类似于is.na的函数,那么我可以在ifelse语句中包含。

IE,

df$new_variable <- ifelse(if.last(df) == 'T' & df$B == 25, 0, df$B)

I am looking to obtain a vector of T/F values based on whether I have reached the last row of data or not. I have attached an example data frame below.

df <- structure(list(A = 1:24, B = 2:25), class = "data.frame", row.names = c(NA, -24L))

Here is my desired output given the provided data.

c("F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", 
  "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "T")

Would be great if this was a function similar to is.na such that I could include in an ifelse statement.

i.e.,

df$new_variable <- ifelse(if.last(df) == 'T' & df$B == 25, 0, df$B)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

非要怀念 2025-02-01 04:34:32

您可以尝试以下操作:

1:nrow(df) == nrow(df)

使其成为一个函数:

is.last <- function(data) {
  1:nrow(data) == nrow(data)
}

is.last(df)
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE

You can try this:

1:nrow(df) == nrow(df)

To make it a function:

is.last <- function(data) {
  1:nrow(data) == nrow(data)
}

is.last(df)
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
简单 2025-02-01 04:34:32

容易

df$new_variable <- rep(c(FALSE, TRUE), c(nrow(df)-1, 1))

false 的列

df$new_variable <- FALSE
df$new_variable[nrow(df)] <- TRUE

row(df) == nrow(df)

使用rep或创建柱子

seq_along(df[[1]]) == nrow(df)

It is easier with rep

df$new_variable <- rep(c(FALSE, TRUE), c(nrow(df)-1, 1))

Or create a column of FALSE and assign back the last row to TRUE

df$new_variable <- FALSE
df$new_variable[nrow(df)] <- TRUE

Or if we need a matrix

row(df) == nrow(df)

Or for a single column

seq_along(df[[1]]) == nrow(df)
禾厶谷欠 2025-02-01 04:34:32

为自己创建了一个解决这个简单示例的小功能。

if.last <- function(x) ifelse(row(x) < nrow(x), 'F', 'T')
if.last(df)
      [,1] [,2]
 [1,] "F"  "F" 
 [2,] "F"  "F" 
 [3,] "F"  "F" 
 [4,] "F"  "F" 
 [5,] "F"  "F" 
 [6,] "F"  "F" 
 [7,] "F"  "F" 
 [8,] "F"  "F" 
 [9,] "F"  "F" 
[10,] "F"  "F" 
[11,] "F"  "F" 
[12,] "F"  "F" 
[13,] "F"  "F" 
[14,] "F"  "F" 
[15,] "F"  "F" 
[16,] "F"  "F" 
[17,] "F"  "F" 
[18,] "F"  "F" 
[19,] "F"  "F" 
[20,] "F"  "F" 
[21,] "F"  "F" 
[22,] "F"  "F" 
[23,] "F"  "F" 
[24,] "T"  "T" 
df$new_variable <- ifelse(if.last(df) == 'T', 0, df$B)
tail(df)
    A  B new_variable 
19 19 20             20 
20 20 21             21 
21 21 22             22 
22 22 23             23 
23 23 24             24 
24 24 25              0 

Created a little function for myself that solves this simple example.

if.last <- function(x) ifelse(row(x) < nrow(x), 'F', 'T')
if.last(df)
      [,1] [,2]
 [1,] "F"  "F" 
 [2,] "F"  "F" 
 [3,] "F"  "F" 
 [4,] "F"  "F" 
 [5,] "F"  "F" 
 [6,] "F"  "F" 
 [7,] "F"  "F" 
 [8,] "F"  "F" 
 [9,] "F"  "F" 
[10,] "F"  "F" 
[11,] "F"  "F" 
[12,] "F"  "F" 
[13,] "F"  "F" 
[14,] "F"  "F" 
[15,] "F"  "F" 
[16,] "F"  "F" 
[17,] "F"  "F" 
[18,] "F"  "F" 
[19,] "F"  "F" 
[20,] "F"  "F" 
[21,] "F"  "F" 
[22,] "F"  "F" 
[23,] "F"  "F" 
[24,] "T"  "T" 
df$new_variable <- ifelse(if.last(df) == 'T', 0, df$B)
tail(df)
    A  B new_variable 
19 19 20             20 
20 20 21             21 
21 21 22             22 
22 22 23             23 
23 23 24             24 
24 24 25              0 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文