在r中找到值并替换

时间:2016-04-05 11:51:11

标签: r

我需要找到" 9"在我的数据框中,并将其替换为另一列中的值。

例如我的数据框:

"1" "total_1_SNP20001" "mu" 1 1922 1369.25 1369.25 "." NA
"2" "total_1_SNP20001" "person" 3 1922 91.52 91.52 "a" NA
"3" "total_1_SNP20001" "barn.level.row" 17 1922 2.85 2.85 "A" NA
"4" "total_1_SNP20001" "9" NA 1 1922 1369.25 "1369.25" NA
"5" "total_1_SNP20002" "mu" 1 1921 1368.62 1346.47 "." NA
"6" "total_1_SNP20002" "person" 3 1921 91.48 91.41 "a" NA
"7" "total_1_SNP20002" "barn.level.row" 17 1921 2.85 2.85 "A" NA
"8" "total_1_SNP20002" "SNP20002" 1 1921 0.12 0.12 "A" 0.72

此处"9" NA 1922 1369.25 "1369.25" NA需要由"SNP20001" 1 1921 0 0 "A" NA替换。部分" SNP20001"需要来自之前的列(但减去total_1_部分),其余的是固定值。我如何在R?中做到这一点?

3 个答案:

答案 0 :(得分:1)

这是一种方法

library(stringr)
df$V3 <- with(df, ifelse(V3==9, str_extract(V2, 'SNP[0-9]+'), V3))
df$V3
#[1] "mu"             "person"         "barn.level.row" "SNP20001"       "mu"             "person"         "barn.level.row" "SNP20002"  

或者,如果您不想使用stringr,那么

df$V3 <- with(df, ifelse(V3==9, sub('.*_([_])*', '', V2), V3))

数据

dput(df)
structure(list(V1 = 1:8, V2 = c("total_1_SNP20001", "total_1_SNP20001", 
"total_1_SNP20001", "total_1_SNP20001", "total_1_SNP20002", "total_1_SNP20002", 
"total_1_SNP20002", "total_1_SNP20002"), V3 = c("mu", "person", 
"barn.level.row", "9", "mu", "person", "barn.level.row", 
"SNP20002"), V4 = c(1L, 3L, 17L, NA, 1L, 3L, 17L, 1L), V5 = c(1922L, 
1922L, 1922L, 1L, 1921L, 1921L, 1921L, 1921L), V6 = c(1369.25, 
91.52, 2.85, 1922, 1368.62, 91.48, 2.85, 0.12), V7 = c(1369.25, 
91.52, 2.85, 1369.25, 1346.47, 91.41, 2.85, 0.12), V8 = structure(c(1L, 
3L, 4L, 2L, 1L, 3L, 4L, 4L), .Label = c(".", "1369.25", "a", 
"A"), class = "factor"), V9 = c(NA, NA, NA, NA, NA, NA, NA, 0.72
)), .Names = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", 
"V9"), row.names = c(NA, -8L), class = "data.frame")

答案 1 :(得分:0)

它有点乱,仅适用于类似于发布的数据框但其工作

的指定案例
df[,3] <-  ifelse(df[,3] == "9", unlist(lapply(strsplit(df[,2],split = "_"), FUN = function(x) x[3])) , df[,3])

可能有更好的方法。

答案 2 :(得分:0)

加载数据

rawdata <- read.table(stringsAsFactors = FALSE,
text = '"1" "total_1_SNP20001" "mu" 1 1922 1369.25 1369.25 "." NA
"2" "total_1_SNP20001" "person" 3 1922 91.52 91.52 "a" NA
"3" "total_1_SNP20001" "barn.level.row" 17 1922 2.85 2.85 "A" NA
"4" "total_1_SNP20001" "9" NA 1 1922 1369.25 "1369.25" NA
"5" "total_1_SNP20002" "mu" 1 1921 1368.62 1346.47 "." NA
"6" "total_1_SNP20002" "person" 3 1921 91.48 91.41 "a" NA
"7" "total_1_SNP20002" "barn.level.row" 17 1921 2.85 2.85 "A" NA
"8" "total_1_SNP20002" "SNP20002" 1 1921 0.12 0.12 "A" 0.72')

仅编辑第3列

如果您只想编辑第3列,则可以 将V3中的“9”替换为V2之前的值

modified1 <- within(rawdata, V3 <- ifelse(V3 == "9", V2, V3))
# Remove  "total_1_" part
modified1 <- within(modified1, V3 <- gsub("total_1_", "", V3))

编辑第3列和其他列

但您也想添加添加固定值。 然后,最好提取要编辑的行 并立即编辑它们

editedlines <- subset(rawdata, V3 == "9")
editedlines <- within(editedlines, {
    V3 <- gsub("total_1_", "", V2)
    V4 <- 1
    V5 <- 1921
    V6 <- 0
    V7 <- 0
    V8 <- "A"
    V9 <- NA})
# Put editedlines back with the rest of the unmodified data
modified2 <- rbind(subset(rawdata, V3 != "9"),
                  editedlines)
# Arrange according to V1 if you prefer
modified2 <- modified2[order(modified2$V1),]