gpt4 book ai didi

r - 如何根据元素情况删除一行?

转载 作者:行者123 更新时间:2023-12-01 23:23:04 25 4
gpt4 key购买 nike

我有一个像下面这样的数据框

V1        V2       V3        V4      V5
ATP1A2 ATP1A1 220948_s_at LARS2 204016_at
CAPRIN1 ATP1A2 LARS2
ATP1A1 ATP1A2 203296_s_at MTHFD1 202309_at
CBX3 ATP1B1 201243_s_at PRKCSH 200707_at
AUP1 ATP1B1 PRKCSH 214080_x_at
LARS2 AUP1 220525_s_at PRKCSH
MTHFD1 AUP1 VDAC2 211662_s_at
VDAC2 B3GNT3 204856_at VDAC2
PRKCSH B3GNT3
ATP1B1 CAPRIN1 200722_s_at
B3GNT3 CAPRIN1 200723_s_at
CAPRIN1
CBX3 200037_s_at

我想从 V2 中删除 V3 为空的那些元素,或者从 V4 中删除 V5 为空的元素,例如在 V3 中,第二个元素为空,因此应该删除 V3 和 V2 的第二个元素。输出结果如下所示。

     V1        V2       V3         V4      V5
ATP1A2 ATP1A1 220948_s_at LARS2 204016_at
CAPRIN1 ATP1A2 203296_s_at MTHFD1 202309_at
ATP1A1 ATP1B1 201243_s_at PRKCSH 200707_at
CBX3 AUP1 220525_s_at PRKCSH 214080_x_at
AUP1 B3GNT3 204856_at VDAC2 211662_s_at
LARS2 CAPRIN1 200722_s_at
MTHFD1 CAPRIN1 200723_s_at
VDAC2 CBX3 200037_s_at
PRKCSH
ATP1B1
B3GNT3

我可以检测到如何在每一列中找到那些空字符串,但问题是如何根据它删除两列行并对所有数据框执行此操作

# to find the empty elements 
Rcolumn3 <-which(df$V3=="")
Rcolumn5 <-which(df$V5=="")

df<- structure(list(V1 = structure(c(3L, 7L, 2L, 8L, 5L, 9L, 10L,
12L, 11L, 4L, 6L, 1L, 1L), .Label = c("", "ATP1A1", "ATP1A2",
"ATP1B1", "AUP1", "B3GNT3", "CAPRIN1", "CBX3", "LARS2", "MTHFD1",
"PRKCSH", "VDAC2"), class = "factor"), V2 = structure(c(1L, 2L,
2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 7L), .Label = c("ATP1A1",
"ATP1A2", "ATP1B1", "AUP1", "B3GNT3", "CAPRIN1", "CBX3"), class = "factor"),
V3 = structure(c(9L, 1L, 6L, 5L, 1L, 8L, 1L, 7L, 1L, 3L,
4L, 1L, 2L), .Label = c("", "200037_s_at", "200722_s_at",
"200723_s_at", "201243_s_at", "203296_s_at", "204856_at",
"220525_s_at", "220948_s_at"), class = "factor"), V4 = structure(c(2L,
2L, 3L, 4L, 4L, 4L, 5L, 5L, 1L, 1L, 1L, 1L, 1L), .Label = c("",
"LARS2", "MTHFD1", "PRKCSH", "VDAC2"), class = "factor"),
V5 = structure(c(4L, 1L, 3L, 2L, 6L, 1L, 5L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("", "200707_at", "202309_at", "204016_at",
"211662_s_at", "214080_x_at"), class = "factor")), .Names = c("V1",
"V2", "V3", "V4", "V5"), class = "data.frame", row.names = c(NA,
-13L))

获取数据

最佳答案

这是一种方式:

#convert to character
df[] <- lapply(df, as.character)

#convert the equivalent V2 or V4 columns' rows into ''
a <- which(df$V3=='')
b <- which(df$V5=='')

df$V2[a] <- ''
df$V4[b] <- ''

此时我已经编写了一个将空白推到向量/列的末尾/底部的函数:

push_up <- function(vec){
c(vec[-which(vec=='')], vec[which(vec=='')])
}

并将其应用到 df:

df[] <- lapply(df, push_up)

输出:

> df
V1 V2 V3 V4 V5
1 ATP1A2 ATP1A1 220948_s_at LARS2 204016_at
2 CAPRIN1 ATP1A2 203296_s_at MTHFD1 202309_at
3 ATP1A1 ATP1B1 201243_s_at PRKCSH 200707_at
4 CBX3 AUP1 220525_s_at PRKCSH 214080_x_at
5 AUP1 B3GNT3 204856_at VDAC2 211662_s_at
6 LARS2 CAPRIN1 200722_s_at
7 MTHFD1 CAPRIN1 200723_s_at
8 VDAC2 CBX3 200037_s_at
9 PRKCSH
10 ATP1B1
11 B3GNT3
12
13

df[df$V1 != '',] 删除第 12,13 行

关于r - 如何根据元素情况删除一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28992531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com