Кажется, что l oop в моей функции R работает дважды - PullRequest
1 голос
/ 20 января 2020

Мне нужно добавить строки во фрейм данных. У меня много файлов с множеством строк, поэтому я преобразовал код в функцию. Когда я go через каждый элемент кода, он работает нормально. Когда я заключаю все в функцию, каждая строка из моего первого l oop добавляется дважды.

Мой код ищет строку (xx или x). Если xx присутствует, заменяет xx номерами 00-99 (по одной строке на каждое число) и 0-9. Если x присутствует, он заменяет его номером 0-9.

Создать DF

a <- c("1.x", "2.xx", "3.1")
b <- c("single", "double", "nothing")
df <- data.frame(a, b, stringsAsFactors = FALSE)
names(df) <- c("code", "desc")

Мой фрейм данных

 code    desc
1  1.x  single
2 2.xx  double
3  3.1 nothing

Моя функция


newdf <- function(df){

    # If I run through my code chunk by chunk it works as I want it.

    df$expanded <- 0 # a variable to let me know if the loop was run on the row

    emp <- function(){ # This function creates empty vectors for my loop
        assign("codes", c(), envir = .GlobalEnv)
        assign("desc", c(), envir = .GlobalEnv)
        assign("expanded", c(), envir = .GlobalEnv)
    }

    emp()

    # I want to expand xx with numbers 00 - 99 and 0 - 9. 
    #Note: 2.0 is different than 2.00

    # Identifies the rows to be expanded    
    xd <- grep("xx", df$code)

    # I used chr vs. numeric so I wouldn't lose the trailing zero
    # Create a vector to loop through
    tens <- formatC(c(0:99)); tens <- tens[11:100]
    ones <- c("00","01","02","03","04","05","06","07","08","09")
    single <- as.character(c(0:9))
    exp <- c(single, ones, tens)

    # This loop appears to run twice when I run the function: newdf(df) 
    # Each row is there twice: 2.00, 2.00, 2.01 2.01... 
    # It runs as I want it to if I just highlight the code. 

    for (i in xd){
        for (n in exp) {
            codes <- c(codes, gsub("xx", n, df$code[i])) #expanding the number
            desc <- c(desc, df$desc[i])  # repeating the description
            expanded <- c(expanded, 1) # assigning 1 to indicated the row has been expanded
        }
    }

    # Binds the df with the new expansion
    df <- df[-xd, ]
    df <- rbind(as.matrix(df),cbind(codes,desc,expanded))
    df <- as.data.frame(df, stringsAsFactors = FALSE)


    # Empties the vector to begin another expansion
    emp()
    xs <- grep("x", df$code) # This is for the single digit expansion

    # Expands the single digits. This part of the code works fine inside the function.
    for (i in xs){
        for (n in 0:9) {
            codes <- c(codes, gsub("x", n, df$code[i]))
            desc <- c(desc, df$desc[i])
            expanded <- c(expanded, 1)
        }
    }

    df <- df[-xs,]
    df <- rbind(as.matrix(df), cbind(codes,desc,expanded))
    df <- as.data.frame(df, stringsAsFactors = FALSE)

    assign("out", df, envir = .GlobalEnv) # This is how I view my dataframe after I run the function.
}

Вызов моей функции

newdf(df)
...