English 中文(简体)
Transform R dataframes using variables in loop
原标题:

I am trying to replace values in a R dataframe by column. I would like to loop though a given list of columns of the dataframe and replace all "Yes" values by 1 and all the other values by 0.

I tried to do this using transform() and ifelse() functions with the something like this:

# List of selected Columns:
ColumnNames = c("Frigori", "Microond" , "Arca", "Aspira")

# Replace Values in dataframe
for(i in 1:length(ColumnNames)){
dataframe <- transform(dataframe, ColumnNames[i] = ifelse(Columnames[i] == "Yes", 1, 0))
}

This piece of code works fine with explicit column names outside the loop, but with the array it will give me the following error:

Error: unexpected  =  in:
"for(i in 1:length(Appliances)){
dataframe <- transform(dataframe, ColumnNames[i] ="

I don t know what goes wrong here, but the problem has to be related with the variable substitution.

问题回答

The code can actually be simplified to one short line with no loops or apply() at all:

dataframe <- data.frame(a = c("No", "Yes", "No", "No", "Yes"),
                        b = c("Hi", "Hi", "Mom", "Hi", "Mom"),
                        c = c("Yes", "Yes", "Yes", "Yes", "No"))
cols <- c("a","c")
dataframe[,cols] <- as.numeric(dataframe[,cols]=="Yes")
dataframe

  a   b c
1 0  Hi 1
2 1  Hi 1
3 0 Mom 1
4 0  Hi 1
5 1 Mom 0

Simulated data:

data <- data.frame(matrix(ifelse(runif(40)>.5,"YES",letters[1:26]), 10, 4))

Suppose you want to change columns X2 and X4

cols <- c("X2","X4")
data[,cols] <- apply(data[cols],2,function(x) ifelse(x=="YES",1,0))




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签