美文网首页R语言学习笔记
R语言学习笔记4-列表篇

R语言学习笔记4-列表篇

作者: RudyHe | 来源:发表于2015-12-03 21:12 被阅读207次
    - List operations
        - j<-list(name="Joe",salary=55000,union=T)
        - j$sal    # only prefix can be recognized
        - j[["salary"]]    # same as above
        - j[[2]]    # same as above
        - z<-vector(mode="list")
        - z[["abc"]]<-3
        - j[1:2]    # Joe 55000
        - j2<-j[2]
        - class(j2)    # "list"
        - j[[1:2]]    # ERROR [[]] is only for one element
        - j2a<-j[[2]]
        - class(j2a)    # "numeric"
        - names(j)    # "name" "salary" "union"
        - ulj<-unlist(j)    # vector of string, "Joe" "55000" "T"
        - y<-unlist(list(a=5,b=12,c=13))    # class(y)="numeric"
        - names(wu)<-NULL    # same as unname(wu)
        - lapply(list(1:3,25:29),median)    # list apply
            - [[1]] 2
            - [[2]] 27
        - sapply(list(1:3,25:29),median)    # simplified apply
            - 2 27
        - x<-c(12,5,13,8)
        - order(x)    # 2 4 1 3
        - b<-list(u=5,v=12)
        - c<-list(w=13)
        - a<-list(b,c)
        - a
            - [[1]]
            - [[1]]$u
            - 5
            - [[1]]$v
            - 12
        - length(a)    # 2
        - c(list(a=1,b=2,c=list(d=5,e=9)))
            - $a
            - 1
            - $b
            - 2
            - $c
            - $c$d
            - 5
            - $c$e
            - 9 
        - c(list(a=1,b=2,c=list(d=5,e=9)),recursive=T)    # Non recursive list, T is abbr of TRUE, default is FALSE
            - a b c.d c.e
            - 1 2 5 9
    
    - add & remove
        - z<-list(a="abc",b=12)
        - z$c<-"sailing"    # add by key
        - z[[4]]<-28    # add by index
        - z[5:7]<-c(FALSE,TRUE,TRUE)    #add vector
        - z$b<-NULL    # remove both key and value and all index after b substract 1
        - z[[4]]    # FALSE not 28
        - c(list("Joe",55000,T),list(5))    # merge list
        - length(j)    # 3
    
    - example
        findwords<-function(tf) {
            txt<-scan(tf,"")
            wl<-list()
            for (i in 1:length(txt)) {
                wrd<-txt[i]    # ith word in txt
                wl[[wrd]]<-c(wl[[wrd]],i)    # append wrd and its index to wl with key=wrd
            }
            return(wrd)
        }
        findwords("....txt")
    
        alphawl<-function(wrdlist) {
            nms<-names(wrdlst)
            sn<-sort(nms)    # sort by alphabet
            return(wrdlst[sn])
        }
    
        freqwl<-function(wrdlst) {
            freqs<-sapply(wrdlst,length)
            return(wrdlst[order(freqs)])    # after sorted original-index list
        }
    
        nyt<-findwords("nyt.txt")
        snyt<-freqwl(nyt)
        nwords<-length(snyt)
        freqs9<-sapply(snyt[round(0.9*nwords):nwords],length)    # top 10% words hist
        barplot(freqs9)
    
        g<-c("M","F","F","I","M","M","F")
        lapply(c("M","F","I"),function(gender) which(g==gender))    # [[1]] 1 5 6 [[2]] 2 3 7 [[3]] 4, anonymous function
    

    相关文章

      网友评论

        本文标题:R语言学习笔记4-列表篇

        本文链接:https://www.haomeiwen.com/subject/vjjshttx.html