R语言在每行的多列中查找和替换

时间:2015-11-30 18:21:49

标签: r

如果我有下面的csv,并且需要重命名每行中排名最高的“蓝色”团队,我该怎么做呢。完整数据集中超过100,000行。

例如,如果“Blue Team B”排在第4位且“Blue Team A”排在第6位,我想将“Blue Team B”重命名为“Blue Winner”。

Position 1,Position 2,Position 3,Position 4,Position 5,Position 6
Blue Team B,Red Team 1,Green Team 1,Blue Team A,Red Team 2,Green Team 2
Red Team 2,Green Team 2,Green Team 1,Blue Team 1,Blue Team 2,Red Team 1
Green Team 1,Red Team 1,Green Team 2,Blue Team B,Red Team 2,Blue Team A
Green Team 1,Red Team 1,Green Team 2,Blue Team A,Red Team 2,Blue Team B
Blue Team B,Red Team 2,Blue Team A,Green Team 1,Red Team 1,Green Team 2

2 个答案:

答案 0 :(得分:0)

试试这个:

mat <- scan(text="Position 1,Position 2,Position 3,Position 4,Position 5,Position 6
Blue Team B,Red Team 1,Green Team 1,Blue Team A,Red Team 2,Green Team 2
Red Team 2,Green Team 2,Green Team 1,Blue Team 1,Blue Team 2,Red Team 1
Green Team 1,Red Team 1,Green Team 2,Blue Team B,Red Team 2,Blue Team A
Green Team 1,Red Team 1,Green Team 2,Blue Team A,Red Team 2,Blue Team B
Blue Team B,Red Team 2,Blue Team A,Green Team 1,Red Team 1,Green Team 2",
            skip=1,what="character",sep=',')
winner <- apply(mat,1,function(x) grep("Blue",x)[which.min(grep("Blue",x))])
mat[cbind(1:nrow(mat),winner)] <- "Blue Winner"

您可能希望将text="Position 1,Position 2,...替换为file="yourfile.csv"

答案 1 :(得分:0)

假设您的数据如图所示一致,此示例将向您展示如何满足您的需求:

# Setting an option.
options(stringsAsFactors = FALSE)

# Defining the teams. Abbreviating for shortness.
teams <- c("B1", "R1", "G1", "B2", "R1", "G2", "BB", "BA")
items <- 100

# Creating sample data.
data <- data.frame(

  # Getting a random assortment of places for each column. This COULD and probably will overlap, but that's okay for this example.
  pos1 = sample(teams, items, replace = TRUE),
  pos2 = sample(teams, items, replace = TRUE),
  pos3 = sample(teams, items, replace = TRUE),
  pos4 = sample(teams, items, replace = TRUE),
  pos5 = sample(teams, items, replace = TRUE),
  pos6 = sample(teams, items, replace = TRUE)

)

# Saving the data to compare it against later.
dataSave <- data

# Running an apply through the sample data.
data <- apply(data, 1, function(x) {

  first <- grep("B", x)[1] # Getting the first place the occurence of "B". Replace this with "Blue" for your data.
  x[first] <- "B Winner" # Replacing the first B and designating it at as a winner.
  return(x)

})

# Transposing back as apply turned the data and converting back to data frame.
data <- t(data)
data <- as.data.frame(data)
Aaaand,Sam打我一拳,但无论如何都要发布代码。