Extracting nth element from a nested list following strsplit - R

前端 未结 4 1056
醉梦人生
醉梦人生 2021-01-02 17:09

I\'ve been trying to understand how to deal with the output of strsplit a bit better. I often have data such as this that I wish to split:

myd         


        
4条回答
  •  生来不讨喜
    2021-01-02 17:40

    (at least regarding 1D vectors) [ seems to return NA when "i > length(x)" whereas [[ returns an error.

    x = runif(5)
    x[6]
    #[1] NA
    x[[6]]
    #Error in x[[6]] : subscript out of bounds
    

    Digging a bit, do_subset_dflt (i.e. [) calls ExtractSubset where we notice that when a wanted index ("ii") is "> length(x)" NA is returned (a bit modified to be clean):

    if(0 <= ii && ii < nx && ii != NA_INTEGER)
        result[i] = x[ii];
    else
        result[i] = NA_INTEGER;
    

    On the other hand do_subset2_dflt (i.e. [[) returns an error if the wanted index ("offset") is "> length(x)" (modified a bit to be clean):

    if(offset < 0 || offset >= xlength(x)) {
        if(offset < 0 && (isNewList(x)) ...
        else errorcall(call, R_MSG_subs_o_b);
    }
    

    where #define R_MSG_subs_o_b _("subscript out of bounds")

    (I'm not sure about the above code snippets but they do seem relevant based on their returns)

提交回复
热议问题