Replacing zero and one in data frame in R with letters

replace values in r dplyr
replace values in data frame r
r replace values in dataframe conditional
r replace values in dataframe column
r replace 0 with 1
replace row values in r
replace na with 0 in r
extract value from data frame r

I have a data frame (df) which looks like this:

     df$a df$b       
      T   C          1          1          1          1          
      A   G          0          1          1          0          
      C   G          0          0          1          1          
      A   T          0          0          0          0          

I would like to have this output:

      T   C          C          C          C          C          
      A   G          A          G          G          A          
      C   G          C          C          G          G          
      A   T          A          A          A          A

Basically if it is zero it takes the letter in column df$a while if it is 1 it takes the letter in column df$b and the zeros and ones are replaced.

Anybody knows how to do this in R?

You can do this with base functionality - note that I read the data in columns 1 and 2 as characters, not factors.

df <- read.table(text = "       
      T   C          1          1          1          1          
                 A   G          0          1          1          0          
                 C   G          0          0          1          1          
                 A   T          0          0          0          0", header = FALSE, stringsAsFactors = FALSE)

df[, 3:ncol(df)] <- sapply(df[, 3:ncol(df)], function(x) ifelse(x == 1, df[, 2], df[, 1]))
df
#>   V1 V2 V3 V4 V5 V6
#> 1  T  C  C  C  C  C
#> 2  A  G  A  G  G  A
#> 3  C  G  C  C  G  G
#> 4  A  T  A  A  A  A

Created on 2019-01-25 by the reprex package (v0.2.1)

R: Extract or Replace Parts of a Data Frame, i, j, elements to extract or replace. i, j are numeric or character or, for [ only, empty. If [ returns a data frame it will have unique (and non-missing) row names, if necessary logical matrix indexing sw ## adding a column sw["new1"] <- LETTERS[1:5] # adds a character column sw[["new2"]] [Package base version 2.5.0 Index] Note that when the replacement value is an array (including a matrix) it is not treated as a series of columns (as data.frame and as.data.frame do) but inserted as a single column. Warning The default behaviour when only one row is left is equivalent to specifying drop = FALSE .

df[, -(1:2)] <- ifelse(df[, -(1:2)] == 1, df[, 2], df[, 1])

Extract or Replace Parts of a Data Frame, S3 method for class 'data.frame' x[i, j, drop = ] ## S3 replacement method for class A suitable replacement value: it will be repeated a whole number of times if matrix indexing sw ## adding a column sw["new1"] <- LETTERS[1:5] # adds a r <- NextMethod("[") mostattributes(r) <- attributes(x) r } d <- data.frame(i = 0:7,  replace replaces the values in x with indices given in list by those given in values . If necessary, the values in <code>values</code> are recycled.</p>

Here is a base R solution.

df[-(1:2)] <- t(apply(df, 1, function(x) {
  y <- as.numeric(x[-(1:2)])
  x[1:2][y + 1]
}))

df
#  a b c d e f
#1 T C C C C C
#2 A G A G G A
#3 C G C C G G
#4 A T A A A A

Data.

df <- read.table(text = "
a   b          c          d          e          f
T   C          1          1          1          1          
A   G          0          1          1          0          
C   G          0          0          1          1          
A   T          0          0          0          0   
", header = TRUE)

Extract.data.frame: Extract or Replace Parts of a Data Frame, Extract or replace subsets of data frames. when replacement values are coerced is a complicated one, and one that has changed during R's development​. The output of the previous R code is a new data frame with the name data_new. As you can see, this data frame consists of only three columns. The all-NA variables x3 and x5 were executed. Video & Further Resources. I have recently published a video on my YouTube channel, which shows the R programming code of this tutorial. You can find the

R Replace NA with 0 (10 Examples for Data Frame, Vector & Column), A common way to treat missing values in R is to replace NA with 0. You will find a summary of the most popular approaches in the following. Choose one  R’s data frames regularly create somewhat of a furor on public forums like Stack Overflow and Reddit. Starting R users often experience problems with the data frame in R and it doesn’t always seem to be straightforward. But does it really need to be so? Well, not necessarily. With today’s post, DataCamp wants to show […] The post 15 Easy Solutions To Your Data Frame Problems In R

How to replace all occurrences of a character in a character column , 0 votes. If you used sub() to replace the string, then use gsub() function instead of sub() with the same syntax to replace all occurrences How to spilt a column of a data frame into multiple columns How to sort a data frame by columns in R? Password is requiredPassword should be atleast 8 characters. Data Science Stack Exchange is a question and answer site for Data science professionals, Machine Learning specialists, and those interested in learning more about the field. It only takes a minute to sign up.

Using vectors and matrices in R, You can see that the numbers have been changed to characters because they are or set it using the setwd function or File -> Change dir selection in the R console. R will also recognize the unquoted string NA as a missing value when data is A data frame is like a matrix in that it represents a rectangular array of data,  The story over when replacement values are coerced is a complicated one, and one that has changed during R's development. This section is a guide only. This section is a guide only. When [ and [[ are used to add or replace a whole column, no coercion takes place but value will be replicated (by calling the generic function rep ) to the right

Comments
  • It is unclear what structure your data has, but you could use something like ifelse(indicator, df$b, df$a) where indicator is the matrix of 1/0 indicators (which R will interpret as 0=FALSE, 1=TRUE)