2016-04-05 59 views
0

我需要在我的數據框中找到「9」,並將其替換爲站在另一列中的值。查找值並在r中替換

例如我的數據框:

"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中做到這一點?

+0

您可以使用此柱更換編輯所有行,你只需要調整它自己的代碼:'IR < - irse','ifelse(ir $ Petal.Length == 1.4,ir $ Sepal.Length [ir $ Petal.Length == 1.4],「Test」)'然後你可以使用splitstr函數來分割第一列。 – Bas

+0

如果你用'dput(dataframe)'發佈你的數據框的一部分'我可以幫你進一步 – Bas

+0

'gsub'函數可以用來替換值。 –

回答

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,你可以 替換爲 「9」,在V3由之前的列中的值,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),] 
0

它有點亂,只能在類似於發佈的數據幀,但其工作

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

有可能是做一個更好的方法規定的情況下工作。

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)) 

DATA

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")