简体   繁体   English

使用循环变量来转换R数据帧

[英]Transform R dataframes using variables in loop

I am trying to replace values in a R dataframe by column. 我试图按列替换R数据框中的值。 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. 我想遍历数据框的给定列表,并将所有“是”值替换为1,并将所有其他值替换为0。

I tried to do this using transform() and ifelse() functions with the something like this: 我试图使用transform()和ifelse()函数来完成此操作,如下所示:

# 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: 实际上,可以将代码简化为一小段代码,没有循环,也没有apply()

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 假设您要更改列X2和X4

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM