开发者

How to get list of lists in R with my data

here is the function which is working fine, but giving the output开发者_StackOverflow社区 in an output which I don't want.

frequencies <- {}

for (k in (1:4))
{   
    interval <- (t(max_period_set[k]))

    intervals <- round(quantile(interval,c(0,0.05,0.15,0.25,0.35,0.45,0.55,0.65,0.75,0.85,0.95,1.0)))
    frequency <- {}
    for (i in (2:length(intervals)))
    {       
        count = 0;      
        for (r in (1:length(interval)))
        {
            if (r == length(interval))
            {
                if (interval[r] >= intervals[i-1] && interval[r] <= intervals[i])
                {
                    count = count + 1
                }
            }
            else
            { 
                if (interval[r] >= intervals[i-1] && interval[r] < intervals[i])
                {
                    count = count + 1
                }
            }
        }   
        frequency <- c(frequency,count)
    }
    frequencies[[length(frequencies)+1]] <- frequency
}

The output is as follows:

> frequencies
[[1]]
 [1] 2 6 5 4 5 4 6 5 5 5 3

[[2]]
 [1] 1 7 5 4 5 4 5 6 5 5 3

[[3]]
 [1] 3 5 5 4 5 4 6 5 5 5 3

[[4]]
 [1] 3 5 5 4 4 6 5 5 5 5 3

I would like to have it in a format as follows:

[[],[],[],[]] which is a list of list whose first element I can access like frequencies[1] to get the first list, etc...

If it is not possible, how can I access the first list values in my current format? frequencies[1] does not give me the first list values back.

Thanks for your help!

Guys an another question:

now I can access the data but r is representing the last line in different format:

[[1]]
 [1] 1.00 0.96 0.84 0.74 0.66 0.56 0.48 0.36 0.26 0.16 0.06 0.00

[[2]]
 [1] 1.00 0.98 0.84 0.74 0.66 0.56 0.48 0.38 0.26 0.16 0.06 0.00

[[3]]
 [1] 1.00 0.94 0.84 0.74 0.66 0.56 0.48 0.36 0.26 0.16 0.06 0.00

[[4]]
 [1] 1.000000e+00 9.400000e-01 8.400000e-01 7.400000e-01 6.600000e-01 5.800000e-01 4.600000e-01 3.600000e-01 2.600000e-01 1.600000e-01 6.000000e-02 1.110223e-16

Why is it happening with the accuracy? the first three lines are as it should be but the last line is odd, the numbers were not infractional numbers, so it can be represented as a number with its accuracy of 2 after comma digits.


frequencies is a list, so you need

 frequencies[[1]]

to access the first element. If list were named, you could also index by element name.

Lists are the most general data structure, and the only one that can

  • be nested: lists within lists within ...
  • be ragged: does not require rectangular dimensions

so you should try to overcome initial aversion to the fact that it is different. These are very powerful data structures, and are use a lot behind the scenes.

Edit: Also, a number of base functions as well as add-on packages can post-process lists. It starts with something basic like do.call(), goes to lapply and ends all the over at the plyr packages. Keep reading -- there are many ways to skin the same cat, some better than others.


While I completely agree with Dirk on the usefulness of lists, you can, if all of your lists are the same length, convert them to a dataframe using as.data.frame() and then you can index them by column i frequencies[,i] or by row j frequencies[j,]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜