Как не использовать вложенный цикл for и улучшить мой код R? - PullRequest
0 голосов
/ 29 апреля 2019

У меня есть вложенный цикл в приведенном ниже коде.

Это зацикливается на каждом столбце и каждой строке - есть ли простой способ векторизовать это?

FYI - содержимое цикла проверяет, содержит ли список в каждой записи только NA, и, таким образом, можно удалить весь столбец.


# install.packages("rtweet")
library("rtweet")             
rbloggers <- get_timeline(user = "Rbloggers", n = 10000)
View(rbloggers)
# install.packages("janitor")
library("janitor")             

rbloggers <- janitor::remove_empty(rbloggers, which = "cols")
# this removes the columns with NA or blank - which are not in lists.

# readr::write_csv - would like to use this later and this cannot handle vector of type list.

rbloggers <- as.data.frame(rbloggers)

for (j in 1:ncol(rbloggers)){

    x <- 0
    for (i in 1:nrow(rbloggers)){
      x <- x + all(is.na(rbloggers[i,j][[1]]))
    }

    # if every element is NA, then remove the column
    if(x == nrow(rbloggers)) {rbloggers[,j] <- NULL}

                            # Many ways to remove a column:
                            # # Data[2] <- NULL
                            # # Data[[2]] <- NULL
                            # # Data <- Data[,-2]
                            # # Data <- Data[-2]
}


К вашему сведению - я пытаюсь понять следующие ссылки:

1 Ответ

0 голосов
/ 29 апреля 2019
library(rtweet)             
rbloggers <- get_timeline(user = "Rbloggers", n = 10000)

library(janitor)             

rbloggers <- janitor::remove_empty(rbloggers, which = "cols")

# find the sum of NA in each col
colSums(is.na(rbloggers))
#>                user_id              status_id             created_at 
#>                      0                      0                      0 
#>            screen_name                   text                 source 
#>                      0                      0                      0 
#>     display_text_width               is_quote             is_retweet 
#>                      0                      0                      0 
#>         favorite_count          retweet_count               hashtags 
#>                      0                      0                      0 
#>               urls_url              urls_t.co      urls_expanded_url 
#>                      0                      0                      0 
#>       mentions_user_id   mentions_screen_name                   lang 
#>                   3175                   3175                      0 
#>             geo_coords          coords_coords            bbox_coords 
#>                      0                      0                      0 
#>             status_url                   name               location 
#>                      0                      0                      0 
#>            description                    url              protected 
#>                      0                      0                      0 
#>        followers_count          friends_count           listed_count 
#>                      0                      0                      0 
#>         statuses_count       favourites_count     account_created_at 
#>                      0                      0                      0 
#>               verified            profile_url   profile_expanded_url 
#>                      0                      0                      0 
#>           account_lang profile_background_url      profile_image_url 
#>                      0                      0                      0

library(dplyr)

# remove the cols that consist of NA
rbloggers_clean <- rbloggers %>% 
  select(- mentions_user_id, -mentions_screen_name)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...