anajacintafernandes anajacintafernandes - 7 days ago 5
R Question

Replace character in column with previous column character in R

I have a dataframe of snps and their respective proxies.

I want to replace every time there is a "warning" in the proxy column with the character in the previous column, i.e., in the snp column.

so if i have

snp proxy
rs272653 rs235343
rs35775 WARNING
rs726354 rs28635


i want:

snp proxy
rs272653 rs235343
rs35775 rs35775
rs726354 rs28635


sessionInfo()

R version 3.3.2 (2016-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS

Answer

Before all, thank you all for your answers!
So, my data frame data contained the collums "SNP" and "Proxy". These were factors! Because of that i had to first know exactly which SNPs in the "SNP" column had a "WARNING" in the "Proxy" column and then add it to levels in the "Proxy".
data[data$Proxy == "WARNING", "SNP"]
#[1] rs11571833 rs17879961 rs12048493 rs72755295 rs9257408

levels(data$Proxy) <- c(levels(data$Proxy), "rs11571833", "rs17879961", "rs12048493", "rs72755295", "rs9257408")

Only then would @code_is_entropy answer work! Sorry for the trouble!

data[data$Proxy == "WARNING", "Proxy"] <- data[data$Proxy == "WARNING", "SNP", drop = FALSE]

Cheers!

Comments