r - How to calculate the mean of one variable (sales) in respect to another (id)? -


i have data set looks this:

id          .........   date             sales 19164958    .........   2001-09-01  .... 30 39578413    .........   2001-09-01  .... 75.6 

there 65k observations in data set. data structured in 4 columns: id (which non-consecutive in range of 10 80 millions), churn, date , sales. describes spending of customers 3/4 of year.

now shall calculate average spending of each customer. have been given code:

aggr.data <- merge(data[, lapply(.sd, mean), = c("id"),.sdcols = c("sales")],                     data[, lapply(.sd, mean), = c("id"),.sdcols = c("sales")],                     c("id", "sales")) 

now have problem r not know .sd. can please tell me have change receive results. or know other commands can use average spendings of each id?

thank help

dput(head(tel)) structure(list(id = c(19164958l, 39578413l, 43061957l, 51326773l,  54271247l, 70765025l), churn = c(0l, 0l, 0l, 0l, 0l, 0l), date = structure(c(11566,  11566, 11566, 11566, 11566, 11566), class = "date"), sales = structure(c(522l,  849l, 649l, 649l, 522l, 649l), .label = c("100", "100.2", "100.4",  "100.6", "100.8", "101", "101.2", "101.4", "101.6", "101.8",  "102", "102.4", "102.8", "103", "103.2", "103.4", "103.6", "103.8",  "104", "104.2", "104.4", "104.8", "105", "105.2", "105.6", "105.8",  "106", "106.2", "106.4", "106.6", "106.8", "107", "107.2", "107.4",  "107.6", "108", "108.2", "108.4", "108.6", "108.8", "109", "109.2",  "109.4", "109.6", "109.8", "110", "110.2", "110.4", "110.8",  "111", "111.2", "111.4", "111.6", "111.8", "112", "112.4", "112.6",  "112.8", "113.2", "113.4", "113.6", "114", "114.2", "114.4",  "114.8", "115.2", "115.6", "116", "116.2", "116.4", "116.8",  "117", "117.2", "117.4", "117.6", "117.8", "118", "118.4", "118.8",  "119.2", "119.6", "119.8", "120", "120.4", "120.6", "120.8",  "121.2", "121.4", "121.6", "121.8", "122", "122.2", "122.4",  "122.8", "123", "123.2", "123.6", "123.8", "124", "124.4", "124.8",  "125", "125.2", "125.4", "125.6", "125.8", "126", "126.4", "126.8",  "127", "127.2", "127.6", "127.8", "128", "128.4", "128.8", "129",  "129.2", "129.4", "129.6", "130", "130.2", "130.4", "130.8",  "131.2", "131.4", "131.6", "131.8", "132", "132.4", "132.8",  "133.2", "133.4", "133.6", "133.8", "134", "134.4", "134.8",  "135", "135.2", "135.6", "135.8", "136", "136.2", "136.4", "136.8",  "137.2", "137.6", "138", "138.4", "138.6", "138.8", "139.2",  "139.6", "140", "140.2", "140.4", "140.8", "141.2", "141.4",  "141.6", "142", "142.2", "142.4", "142.6", "142.8", "143.2",  "143.6", "144", "144.2", "144.4", "144.6", "144.8", "145.2",  "145.4", "145.6", "146", "146.4", "146.6", "146.8", "147.2",  "147.6", "147.8", "148", "148.2", "148.4", "148.6", "148.8",  "149.2", "149.6", "149.8", "150", "150.2", "150.4", "150.8",  "151", "151.2", "151.6", "152", "152.4", "152.8", "153", "153.2",  "153.6", "154", "154.4", "154.6", "154.8", "155.2", "155.6",  "155.8", "156", "156.2", "156.4", "156.6", "156.8", "157.2",  "157.4", "157.6", "157.8", "158", "158.4", "158.8", "159.2",  "159.4", "159.6", "160", "160.2", "160.4", "160.8", "161.2",  "161.4", "161.6", "162", "162.4", "162.8", "163", "163.2", "163.6",  "163.8", "164", "164.4", "164.8", "165", "165.2", "165.6", "166",  "166.4", "166.8", "167.2", "167.4", "167.6", "168", "168.4",  "168.8", "169.2", "169.6", "170", "170.2", "170.4", "170.8",  "171", "171.2", "171.6", "172", "172.4", "172.8", "173.2", "173.6",  "173.8", "174", "174.4", "174.8", "175.2", "175.6", "175.8",  "176.4", "176.8", "177", "177.2", "177.6", "178", "178.2", "178.4",  "178.8", "179.2", "179.4", "179.6", "179.8", "180", "180.4",  "180.8", "181", "181.2", "181.6", "182", "182.2", "182.4", "182.8",  "183.2", "183.6", "183.8", "184", "184.4", "184.8", "185.2",  "185.6", "186", "186.4", "187.2", "187.4", "187.6", "187.8",  "188", "188.4", "188.8", "189.2", "189.6", "189.8", "190", "190.4",  "190.8", "191.6", "192", "192.4", "192.8", "193.2", "193.6",  "194", "194.4", "194.8", "195.2", "195.6", "196.4", "196.8",  "197.2", "197.6", "197.8", "198", "198.2", "198.4", "198.8",  "199.2", "199.6", "200", "200.4", "200.8", "201.2", "201.6",  "202", "202.4", "202.8", "203.6", "204", "204.4", "204.8", "205.6",  "206", "206.4", "206.6", "206.8", "207.2", "207.6", "208", "208.4",  "208.8", "209.2", "209.6", "209.8", "210", "210.4", "210.6",  "210.8", "211.2", "211.6", "212.8", "213.2", "213.6", "214",  "214.4", "214.8", "215.2", "215.4", "216", "216.2", "216.8",  "217", "217.2", "217.6", "218.4", "218.8", "219.2", "219.6",  "220", "221", "221.2", "221.6", "221.8", "222", "222.4", "223.2",  "223.6", "224.4", "224.8", "225.2", "225.6", "226", "226.2",  "226.4", "226.6", "227.2", "227.8", "228", "228.4", "228.8",  "229.2", "229.6", "229.8", "230", "230.4", "230.8", "231.6",  "232.2", "232.4", "232.8", "233.2", "233.6", "234.4", "234.8",  "235.6", "235.8", "236", "237.2", "237.4", "237.6", "238", "239.2",  "240.4", "240.8", "241.2", "241.6", "242", "242.4", "243.4",  "243.6", "244.6", "245.2", "245.6", "246", "246.4", "247.2",  "248", "249.6", "250", "250.4", "250.8", "251.2", "251.6", "252.8",  "254.4", "254.8", "255.2", "255.4", "255.6", "256", "256.4",  "256.8", "257.2", "257.6", "258.8", "259.2", "260", "261.6",  "262", "262.4", "262.8", "263.2", "263.6", "264", "264.4", "264.8",  "266", "266.8", "267.2", "267.6", "268.4", "270", "270.2", "270.4",  "271", "271.2", "271.6", "272.4", "272.8", "273.2", "274", "274.4",  "275.2", "275.6", "276", "276.8", "278.8", "279.2", "279.6",  "280", "281.6", "282", "282.6", "283.2", "284.8", "285.6", "287.2",  "289.6", "290.4", "291.2", "293.2", "295.2", "296", "296.8",  "298", "299", "30", "30.2", "30.4", "30.6", "30.8", "300.8",  "301.2", "301.6", "302.8", "303.6", "304", "304.4", "305.2",  "306", "306.4", "307.2", "308.8", "309.2", "31", "31.2", "31.4",  "31.6", "31.8", "310.8", "313.2", "313.6", "314", "315", "315.6",  "316", "316.4", "316.8", "317", "318.4", "319.6", "32", "32.2",  "32.4", "32.6", "32.8", "322", "324.8", "326.4", "326.8", "327.2",  "328.4", "329.2", "329.6", "33", "33.2", "33.4", "33.6", "33.8",  "331.6", "332.4", "332.8", "334", "338.4", "338.6", "339.2",  "34", "34.2", "34.4", "34.6", "34.8", "340", "341.2", "342",  "342.4", "347.2", "347.6", "35", "35.2", "35.4", "35.6", "35.8",  "350", "352.8", "353.2", "354", "354.8", "355.6", "357.6", "36",  "36.2", "36.4", "36.6", "36.8", "360.8", "361.6", "362", "362.4",  "363.6", "365.6", "367.6", "368", "368.4", "369.6", "37", "37.2",  "37.4", "37.6", "37.8", "371.6", "372.4", "375.6", "377", "38",  "38.2", "38.4", "38.6", "38.8", "382.6", "384.8", "385.2", "387.2",  "388", "388.4", "39", "39.2", "39.4", "39.6", "39.8", "390.4",  "391.2", "397.6", "399.6", "40", "40.2", "40.4", "40.6", "40.8",  "405.2", "408.8", "41", "41.2", "41.4", "41.6", "41.8", "411.6",  "414.4", "419.2", "42", "42.2", "42.4", "42.6", "42.8", "43",  "43.2", "43.4", "43.6", "43.8", "430.2", "432.4", "437.2", "438",  "439.6", "44", "44.2", "44.4", "44.6", "44.8", "444.8", "45",  "45.2", "45.4", "45.6", "45.8", "450", "454", "455.6", "46",  "46.2", "46.4", "46.6", "46.8", "47", "47.2", "47.4", "47.6",  "47.8", "473.2", "474", "475.6", "48", "48.2", "48.4", "48.6",  "48.8", "482.4", "49", "49.2", "49.4", "49.6", "49.8", "50",  "50.2", "50.4", "50.6", "50.8", "500", "503.2", "51", "51.2",  "51.4", "51.6", "51.8", "52", "52.2", "52.4", "52.6", "52.8",  "521.6", "53", "53.2", "53.4", "53.6", "53.8", "54", "54.2",  "54.4", "54.6", "54.8", "55", "55.2", "55.4", "55.6", "55.8",  "550", "56", "56.2", "56.4", "56.6", "56.8", "57", "57.2", "57.4",  "57.6", "57.8", "58", "58.2", "58.4", "58.6", "58.8", "59", "59.2",  "59.4", "59.6", "59.8", "60", "60.2", "60.4", "60.6", "60.8",  "61", "61.2", "61.4", "61.6", "61.8", "62", "62.2", "62.4", "62.6",  "62.8", "63", "63.2", "63.4", "63.6", "63.8", "64", "64.2", "64.4",  "64.6", "64.8", "65", "65.2", "65.4", "65.6", "65.8", "66", "66.2",  "66.4", "66.6", "66.8", "67", "67.2", "67.4", "67.6", "67.8",  "68", "68.2", "68.4", "68.6", "68.8", "69", "69.2", "69.4", "69.6",  "69.8", "70", "70.2", "70.4", "70.6", "70.8", "71", "71.2", "71.4",  "71.6", "71.8", "72", "72.2", "72.4", "72.6", "72.8", "73", "73.2",  "73.4", "73.6", "73.8", "74", "74.2", "74.4", "74.6", "74.8",  "75", "75.2", "75.4", "75.6", "75.8", "76", "76.2", "76.4", "76.6",  "76.8", "77", "77.2", "77.4", "77.6", "77.8", "78", "78.2", "78.4",  "78.6", "78.8", "79", "79.2", "79.4", "79.6", "79.8", "80", "80.2",  "80.4", "80.6", "80.8", "81", "81.2", "81.4", "81.6", "81.8",  "82", "82.2", "82.4", "82.6", "82.8", "83", "83.2", "83.4", "83.6",  "83.8", "84", "84.2", "84.4", "84.6", "84.8", "85", "85.2", "85.4",  "85.6", "85.8", "86", "86.4", "86.6", "86.8", "87", "87.2", "87.6",  "87.8", "88", "88.2", "88.4", "88.6", "88.8", "89", "89.2", "89.6",  "89.8", "90", "90.2", "90.4", "90.6", "90.8", "91", "91.2", "91.4",  "91.6", "91.8", "92", "92.2", "92.4", "92.6", "92.8", "93", "93.2",  "93.4", "93.6", "93.8", "94", "94.2", "94.4", "94.6", "94.8",  "95", "95.2", "95.4", "95.6", "95.8", "96", "96.2", "96.4", "96.6",  "96.8", "97", "97.2", "97.4", "97.6", "97.8", "98", "98.2", "98.4",  "98.6", "98.8", "99", "99.2", "99.4", "99.6", "99.8"), class = "factor")), .names = c("id",  "churn", "date", "sales"), row.names = c(na, 6l), class = "data.frame") 

mydf$sales <- as.numeric(as.character(mydf$sales))  

using base r

 tapply(mydf$sales,mydf$id,mean) 

where mydf dataframe

using data.table package

library(data.table) dt<-data.table(mydf) dt[,mean(sales),by=id] 

using plyr package

library(plyr) ddply(mydf,.(id),meansales=mean(sales)) 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -