将功能应用于数据框的每一列

时间:2019-06-29 13:12:16

标签: r

我需要转换多列数据框中的数据,并想找到一种方法可以一次对数据框的所有列进行转换。数值数据的数据转换似乎没有问题。例如:

df <- data.frame(
  co1 = c(5,9,6,1,6),
  co2 = c(8,5,4,6,2), 
  co3 = c(6,5,4,1,2),
  co4 = c(6,1,5,3,2),
  co5 = c(5,1,2,6,8))

我可以使用for循环一次转换所有数据(例如,将所有大于5的值标记为“是”,将所有其他值标记为“否”):

for(i in 1:ncol(df)){
  df[i] <- ifelse(df[i] > 5, "yes", "no")
}

或更简单地说,使用索引:

df[] <- ifelse(df > 5, "yes", "no")

但是,当我有字符数据时,这些方法不起作用。例如,我想将此数据帧中所有以“ A”开头的值转换为“是”:

df <- data.frame(
  co1 = c(paste(sample(LETTERS[1:10],5), sample(LETTERS[1:10],5), sep = "")),
  co2 = c(paste(sample(LETTERS[1:10],5), sample(LETTERS[1:10],5), sep = "")), 
  co3 = c(paste(sample(LETTERS[1:10],5), sample(LETTERS[1:10],5), sep = "")),
  co4 = c(paste(sample(LETTERS[1:10],5), sample(LETTERS[1:10],5), sep = "")),
  co5 = c(paste(sample(LETTERS[1:10],5), sample(LETTERS[1:10],5), sep = "")))
df
  co1 co2 co3 co4 co5
1  JF  GB  ID  EB  DF
2  IA  DD  DA  IF  HD
3  HI  IH  JE  CH  FB
4  GE  JI  CJ  BA  GE
5  BG  EE  GG  AJ  BH

for循环

for(i in 1:ncol(df)){
  df[i] <- ifelse(grepl("^B", df[i]), "yes", "no")
}

以及通过索引的变形会产生相同的错误变换:

df[] <- ifelse(grepl("^B", df), "yes", "no")
df
  co1 co2 co3 co4 co5
1  no  no  no  no  no
2  no  no  no  no  no
3  no  no  no  no  no
4  no  no  no  no  no
5  no  no  no  no  no

任何帮助如何使用字符数据实现正确的转换?

4 个答案:

答案 0 :(得分:3)

有了dplyr,我们可以做到:

 df %>% 
  mutate_all(function(x) ifelse(grepl("^B",x),"Yes","No"))
  co1 co2 co3 co4 co5
1 Yes  No Yes  No  No
2  No  No  No  No  No
3  No  No  No  No  No
4  No  No  No  No  No
5  No  No  No  No Yes

关于帖子(df1)中的数据:

df1 %>% 
   mutate_all(function(x) ifelse(grepl("^B",x),"Yes","No"))
  co1 co2 co3 co4 co5
1  No  No  No  No  No
2  No  No  No  No  No
3  No  No  No  No  No
4  No  No  No Yes  No
5 Yes  No  No  No Yes

数据:

df
  co1 co2 co3 co4 co5
1  BH  IC  BC  HJ  CC
2  CC  DH  CF  GI  HI
3  DB  GE  JI  DA  GD
4  II  CA  EJ  IG  FA
5  JD  JB  IG  EB  BE

答案 1 :(得分:3)

如果您想坚持以R为底,rules_version = '2';可以在这里工作:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // Matches any document in the cities collection as well as any document
    // in a subcollection.
    match /cities/{city}/{document=**} {
      allow read, write: if <condition>;
    }
  }
}

答案 2 :(得分:3)

我们可以unlist数据,然后直接在带有索引的基数R中使用grepl

df[] <- c("No", "Yes")[grepl("^B", unlist(df)) + 1]

df
#  co1 co2 co3 co4 co5
#1  No  No  No  No  No
#2  No Yes  No  No  No
#3  No  No  No Yes  No
#4  No  No  No  No  No
#5  No  No  No  No Yes

数据

set.seed(12345)
df <- data.frame(
  co1 = c(paste(sample(LETTERS[1:10],5), sample(LETTERS[1:10],5), sep = "")),
  co2 = c(paste(sample(LETTERS[1:10],5), sample(LETTERS[1:10],5), sep = "")), 
  co3 = c(paste(sample(LETTERS[1:10],5), sample(LETTERS[1:10],5), sep = "")),
  co4 = c(paste(sample(LETTERS[1:10],5), sample(LETTERS[1:10],5), sep = "")),
  co5 = c(paste(sample(LETTERS[1:10],5), sample(LETTERS[1:10],5), sep = "")))

df
#  co1 co2 co3 co4 co5
#1  HB  AE  ED  HD  HD
#2  JC  BD  CG  AH  DA
#3  GE  FI  HE  BI  JI
#4  IF  JB  JB  EE  FH
#5  CG  CF  DC  CA  BJ

答案 3 :(得分:2)

base R中带有substr的选项

out <- array("No", dim = dim(df), dimnames = dimnames(df))
out[substr(as.matrix(df), 1, 1) == "B"] <- "Yes"

数据

df <- structure(list(co1 = structure(c(2L, 4L, 1L, 3L, 5L), .Label = c("BF", 
"CH", "EC", "HB", "JJ"), class = "factor"), co2 = structure(c(3L, 
1L, 4L, 5L, 2L), .Label = c("AD", "FI", "GA", "HH", "JB"), class = "factor"), 
    co3 = structure(c(1L, 5L, 4L, 3L, 2L), .Label = c("CJ", "DB", 
    "EF", "FH", "IG"), class = "factor"), co4 = structure(c(2L, 
    4L, 3L, 1L, 5L), .Label = c("AE", "DH", "HA", "IF", "JC"), class = "factor"), 
    co5 = structure(c(1L, 5L, 3L, 2L, 4L), .Label = c("AC", "BG", 
    "EE", "GI", "JJ"), class = "factor")), 
    class = "data.frame", row.names = c(NA, 
-5L))