Rstudio problem: Some classes have no records

Hi! I'm a university student who must take a mandatory statistic and data science class, a subject that I don't really like :))

I'm stuck on this problem that R keeps presenting me, can someone help?

My dataset is called "London", and has 7375 observations, 7 variables. I'm trying to split the data, but this message keeps appearing:

In createDataPartition(y, p = 0.75, list = FALSE) :
Some classes have no records

How can I erase classes or merge classes in order to avoid classes without records?

Thanks!

Welcome to the forum and happy New Year.

We need a lot more information to help. And don't worry you WILL LEARN To love R in no time :smiling_imp:

We need to see your code and some sample data. See
FAQ Asking Questions

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here between
```

```

Thank you!

I launched this
dput(head(London, 100))
and this is what came out
is this wrong?

`12001` = 12001L, `12002` = 12002L, `12003` = 12003L, `12004` = 12004L, 
`12005` = 12005L, `12006` = 12006L, `12007` = 12007L, `12008` = 12008L, 
`12009` = 12009L, `12010` = 12010L, `12011` = 12011L, `12012` = 12012L, 
`12013` = 12013L, `12014` = 12014L, `12015` = 12015L, `12016` = 12016L, 
`12017` = 12017L, `12018` = 12018L, `12019` = 12019L, `12020` = 12020L, 
`12021` = 12021L, `12022` = 12022L, `12023` = 12023L, `12024` = 12024L, 
`12025` = 12025L, `12026` = 12026L, `12027` = 12027L, `12028` = 12028L, 
`12029` = 12029L, `12030` = 12030L, `12031` = 12031L, `12032` = 12032L, 
`12033` = 12033L, `12034` = 12034L, `12035` = 12035L, `12036` = 12036L, 
`12037` = 12037L, `12038` = 12038L, `12039` = 12039L, `12040` = 12040L, 
`12041` = 12041L, `12042` = 12042L, `12043` = 12043L, `12044` = 12044L, 
`12045` = 12045L, `12046` = 12046L, `12047` = 12047L, `12048` = 12048L, 
`12049` = 12049L, `12050` = 12050L, `12051` = 12051L, `12052` = 12052L, 
`12053` = 12053L, `12054` = 12054L, `12055` = 12055L, `12056` = 12056L, 
`12057` = 12057L, `12058` = 12058L, `12059` = 12059L, `12060` = 12060L, 
`12061` = 12061L, `12062` = 12062L, `12063` = 12063L, `12064` = 12064L, 
`12065` = 12065L, `12066` = 12066L, `12067` = 12067L, `12068` = 12068L, 
`12069` = 12069L, `12070` = 12070L, `12071` = 12071L, `12072` = 12072L, 
`12073` = 12073L, `12074` = 12074L, `12075` = 12075L, `12076` = 12076L, 
`12077` = 12077L, `12078` = 12078L, `12079` = 12079L, `12080` = 12080L, 
`12081` = 12081L, `12082` = 12082L, `12083` = 12083L, `12084` = 12084L, 
`12085` = 12085L, `12086` = 12086L, `12087` = 12087L, `12088` = 12088L, 
`12089` = 12089L, `12090` = 12090L, `12091` = 12091L, `12092` = 12092L, 
`12093` = 12093L, `12094` = 12094L, `12095` = 12095L, `12096` = 12096L, 
`12097` = 12097L, `12098` = 12098L, `12099` = 12099L, `12100` = 12100L, 
`12101` = 12101L, `12102` = 12102L, `12103` = 12103L, `12104` = 12104L, 
`12105` = 12105L, `12106` = 12106L, `12107` = 12107L, `12108` = 12108L, 
`12109` = 12109L, `12110` = 12110L, `12111` = 12111L, `12112` = 12112L, 
`12113` = 12113L, `12114` = 12114L, `12115` = 12115L, `12116` = 12116L, 
`12117` = 12117L, `12118` = 12118L, `12119` = 12119L, `12120` = 12120L, 
`12121` = 12121L, `12122` = 12122L, `12123` = 12123L, `12124` = 12124L, 
`12125` = 12125L, `12126` = 12126L, `12127` = 12127L, `12128` = 12128L, 
`12129` = 12129L, `12130` = 12130L, `12131` = 12131L, `12132` = 12132L, 
`12133` = 12133L, `12134` = 12134L, `12135` = 12135L, `12136` = 12136L, 
`12137` = 12137L, `12138` = 12138L, `12139` = 12139L, `12140` = 12140L, 
`12141` = 12141L, `12142` = 12142L, `12143` = 12143L, `12144` = 12144L, 
`12145` = 12145L, `12146` = 12146L, `12147` = 12147L, `12148` = 12148L, 
`12149` = 12149L, `12150` = 12150L, `12151` = 12151L, `12152` = 12152L, 
`12153` = 12153L, `12154` = 12154L, `12155` = 12155L, `12156` = 12156L, 
`12157` = 12157L, `12158` = 12158L, `12159` = 12159L, `12160` = 12160L, 
`12161` = 12161L, `12162` = 12162L, `12163` = 12163L, `12164` = 12164L, 
`12165` = 12165L, `12166` = 12166L, `12167` = 12167L, `12168` = 12168L, 
`12169` = 12169L, `12170` = 12170L, `12171` = 12171L, `12172` = 12172L, 
`12173` = 12173L, `12174` = 12174L, `12175` = 12175L, `12176` = 12176L, 
`12177` = 12177L, `12178` = 12178L, `12179` = 12179L, `12180` = 12180L, 
`12181` = 12181L, `12182` = 12182L, `12183` = 12183L, `12184` = 12184L, 
`12185` = 12185L, `12186` = 12186L, `12187` = 12187L, `12188` = 12188L, 
`12189` = 12189L, `12190` = 12190L, `12191` = 12191L, `12192` = 12192L, 
`12193` = 12193L, `12194` = 12194L, `12195` = 12195L, `12196` = 12196L, 
`12197` = 12197L, `12198` = 12198L, `12199` = 12199L, `12200` = 12200L, 
`12201` = 12201L, `12202` = 12202L, `12203` = 12203L, `12204` = 12204L, 
`12205` = 12205L, `12206` = 12206L, `12207` = 12207L, `12208` = 12208L, 
`12209` = 12209L, `12210` = 12210L, `12211` = 12211L, `12212` = 12212L, 
`12213` = 12213L, `12214` = 12214L, `12215` = 12215L, `12216` = 12216L, 
`12217` = 12217L, `12218` = 12218L, `12219` = 12219L, `12220` = 12220L, 
`12221` = 12221L, `12222` = 12222L, `12223` = 12223L, `12224` = 12224L, 
`12225` = 12225L, `12226` = 12226L, `12227` = 12227L, `12228` = 12228L, 
`12229` = 12229L, `12230` = 12230L, `12231` = 12231L, `12232` = 12232L, 
`12233` = 12233L, `12234` = 12234L, `12235` = 12235L, `12236` = 12236L, 
`12237` = 12237L, `12238` = 12238L, `12239` = 12239L, `12240` = 12240L, 
`12241` = 12241L, `12242` = 12242L, `12243` = 12243L, `12244` = 12244L, 
`12245` = 12245L, `12246` = 12246L, `12247` = 12247L, `12248` = 12248L, 
`12249` = 12249L, `12250` = 12250L, `12251` = 12251L, `12252` = 12252L, 
`12253` = 12253L, `12254` = 12254L, `12255` = 12255L, `12256` = 12256L, 
`12257` = 12257L, `12258` = 12258L, `12259` = 12259L, `12260` = 12260L, 
`12261` = 12261L, `12262` = 12262L, `12263` = 12263L, `12264` = 12264L, 
`12265` = 12265L, `12266` = 12266L, `12267` = 12267L, `12268` = 12268L, 
`12269` = 12269L, `12270` = 12270L, `12271` = 12271L, `12272` = 12272L, 
`12273` = 12273L, `12274` = 12274L, `12275` = 12275L, `12276` = 12276L, 
`12277` = 12277L, `12278` = 12278L, `12279` = 12279L, `12280` = 12280L, 
`12281` = 12281L, `12282` = 12282L, `12283` = 12283L, `12284` = 12284L, 
`12285` = 12285L, `12286` = 12286L, `12287` = 12287L, `12288` = 12288L, 
`12289` = 12289L, `12290` = 12290L, `12291` = 12291L, `12292` = 12292L, 
`12293` = 12293L, `12294` = 12294L, `12295` = 12295L, `12296` = 12296L, 
`12297` = 12297L, `12298` = 12298L, `12299` = 12299L, `12300` = 12300L, 
`12301` = 12301L, `12302` = 12302L, `12303` = 12303L, `12304` = 12304L, 
`12305` = 12305L, `12306` = 12306L, `12307` = 12307L, `12308` = 12308L, 
`12309` = 12309L, `12310` = 12310L, `12311` = 12311L, `12312` = 12312L, 
`12313` = 12313L, `12314` = 12314L, `12315` = 12315L, `12316` = 12316L, 
`12317` = 12317L, `12318` = 12318L, `12319` = 12319L, `12320` = 12320L, 
`12321` = 12321L, `12322` = 12322L, `12323` = 12323L, `12324` = 12324L, 
`12325` = 12325L, `12326` = 12326L, `12327` = 12327L, `12328` = 12328L, 
`12329` = 12329L, `12330` = 12330L, `12331` = 12331L, `12332` = 12332L, 
`12333` = 12333L, `12334` = 12334L, `12335` = 12335L, `12336` = 12336L, 
`12337` = 12337L, `12338` = 12338L, `12339` = 12339L, `12340` = 12340L, 
`12341` = 12341L, `12342` = 12342L, `12343` = 12343L, `12344` = 12344L, 
`12345` = 12345L, `12346` = 12346L, `12347` = 12347L, `12348` = 12348L, 
`12349` = 12349L, `12350` = 12350L, `12351` = 12351L, `12352` = 12352L, 
`12353` = 12353L, `12354` = 12354L, `12355` = 12355L, `12356` = 12356L, 
`12357` = 12357L, `12358` = 12358L, `12359` = 12359L, `12360` = 12360L, 
`12361` = 12361L, `12362` = 12362L, `12363` = 12363L, `12364` = 12364L, 
`12365` = 12365L, `12366` = 12366L, `12367` = 12367L, `12368` = 12368L, 
`12369` = 12369L, `12370` = 12370L, `12371` = 12371L, `12372` = 12372L, 
`12373` = 12373L, `12374` = 12374L, `12375` = 12375L, `12376` = 12376L, 
`12377` = 12377L, `12378` = 12378L, `12379` = 12379L, `12380` = 12380L, 
`12381` = 12381L, `12382` = 12382L, `12383` = 12383L, `12384` = 12384L, 
`12385` = 12385L, `12386` = 12386L, `12387` = 12387L, `12388` = 12388L, 
`12389` = 12389L, `12390` = 12390L, `12391` = 12391L, `12392` = 12392L, 
`12393` = 12393L, `12394` = 12394L, `12395` = 12395L, `12396` = 12396L, 
`12397` = 12397L, `12398` = 12398L, `12399` = 12399L, `12400` = 12400L, 
`12401` = 12401L, `12402` = 12402L, `12403` = 12403L, `12404` = 12404L, 
`12405` = 12405L, `12406` = 12406L, `12407` = 12407L, `12408` = 12408L, 
`12409` = 12409L, `12410` = 12410L, `12411` = 12411L, `12412` = 12412L, 
`12413` = 12413L, `12414` = 12414L, `12415` = 12415L, `12416` = 12416L, 
`12417` = 12417L, `12418` = 12418L, `12419` = 12419L, `12420` = 12420L, 
`12421` = 12421L, `12422` = 12422L, `12423` = 12423L, `12424` = 12424L, 
`12425` = 12425L, `12426` = 12426L, `12427` = 12427L, `12428` = 12428L, 
`12429` = 12429L, `12430` = 12430L, `12431` = 12431L, `12432` = 12432L, 
`12433` = 12433L, `12434` = 12434L, `12435` = 12435L, `12436` = 12436L, 
`12437` = 12437L, `12438` = 12438L, `12439` = 12439L, `12440` = 12440L, 
`12441` = 12441L, `12442` = 12442L, `12443` = 12443L, `12444` = 12444L, 
`12445` = 12445L, `12446` = 12446L, `12447` = 12447L, `12448` = 12448L, 
`12449` = 12449L, `12450` = 12450L, `12451` = 12451L, `12452` = 12452L, 
`12453` = 12453L, `12454` = 12454L, `12455` = 12455L, `12456` = 12456L, 
`12457` = 12457L, `12458` = 12458L, `12459` = 12459L, `12460` = 12460L, 
`12461` = 12461L, `12462` = 12462L, `12463` = 12463L, `12464` = 12464L, 
`12465` = 12465L, `12466` = 12466L, `12467` = 12467L, `12468` = 12468L, 
`12469` = 12469L, `12470` = 12470L, `12471` = 12471L, `12472` = 12472L, 
`12473` = 12473L, `12474` = 12474L, `12475` = 12475L, `12476` = 12476L, 
`12477` = 12477L, `12478` = 12478L, `12479` = 12479L, `12480` = 12480L, 
`12481` = 12481L, `12482` = 12482L, `12483` = 12483L, `12484` = 12484L, 
`12485` = 12485L, `12486` = 12486L, `12487` = 12487L, `12488` = 12488L, 
`12489` = 12489L, `12490` = 12490L, `12491` = 12491L, `12492` = 12492L, 
`12493` = 12493L, `12494` = 12494L, `12495` = 12495L, `12496` = 12496L, 
`12497` = 12497L, `12498` = 12498L, `12499` = 12499L, `12500` = 12500L, 
`12501` = 12501L, `12502` = 12502L, `12503` = 12503L, `12504` = 12504L, 
`12505` = 12505L, `12506` = 12506L, `12507` = 12507L, `12508` = 12508L, 
`12509` = 12509L, `12510` = 12510L, `12511` = 12511L, `12512` = 12512L, 
`12513` = 12513L, `12514` = 12514L, `12515` = 12515L, `12516` = 12516L, 
`12517` = 12517L, `12518` = 12518L, `12519` = 12519L, `12520` = 12520L, 
`12521` = 12521L, `12522` = 12522L, `12523` = 12523L, `12524` = 12524L, 
`12525` = 12525L, `12526` = 12526L, `12527` = 12527L, `12528` = 12528L, 
`12529` = 12529L, `12530` = 12530L, `12531` = 12531L, `12532` = 12532L, 
`12533` = 12533L, `12534` = 12534L, `12535` = 12535L, `12536` = 12536L, 
`12537` = 12537L, `12538` = 12538L, `12539` = 12539L, `12540` = 12540L, 
`12541` = 12541L, `12542` = 12542L, `12543` = 12543L, `12544` = 12544L, 
`12545` = 12545L, `12546` = 12546L, `12547` = 12547L, `12548` = 12548L, 
`12549` = 12549L, `12550` = 12550L, `12551` = 12551L, `12552` = 12552L, 
`12553` = 12553L, `12554` = 12554L, `12555` = 12555L, `12556` = 12556L, 
`12557` = 12557L, `12558` = 12558L, `12559` = 12559L, `12560` = 12560L, 
`12561` = 12561L, `12562` = 12562L, `12563` = 12563L, `12564` = 12564L, 
`12565` = 12565L, `12566` = 12566L, `12567` = 12567L, `12568` = 12568L, 
`12569` = 12569L, `12570` = 12570L, `12571` = 12571L, `12572` = 12572L, 
`12573` = 12573L, `12574` = 12574L, `12575` = 12575L, `12576` = 12576L, 
`12577` = 12577L, `12578` = 12578L, `12579` = 12579L, `12580` = 12580L, 
`12581` = 12581L, `12582` = 12582L, `12583` = 12583L, `12584` = 12584L, 
`12585` = 12585L, `12586` = 12586L, `12587` = 12587L, `12588` = 12588L, 
`12589` = 12589L, `12590` = 12590L, `12591` = 12591L, `12592` = 12592L, 
`12593` = 12593L, `12594` = 12594L, `12595` = 12595L, `12596` = 12596L, 
`12597` = 12597L, `12598` = 12598L, `12599` = 12599L, `12600` = 12600L, 
`12601` = 12601L, `12602` = 12602L, `12603` = 12603L, `12604` = 12604L, 
`12605` = 12605L, `12606` = 12606L, `12607` = 12607L, `12608` = 12608L, 
`12609` = 12609L, `12610` = 12610L, `12611` = 12611L, `12612` = 12612L, 
`12613` = 12613L, `12614` = 12614L, `12615` = 12615L, `12616` = 12616L, 
`12617` = 12617L, `12618` = 12618L, `12619` = 12619L, `12620` = 12620L, 
`12621` = 12621L, `12622` = 12622L, `12623` = 12623L, `12624` = 12624L, 
`12625` = 12625L, `12626` = 12626L, `12627` = 12627L, `12628` = 12628L, 
`12629` = 12629L, `12630` = 12630L, `12631` = 12631L, `12632` = 12632L, 
`12633` = 12633L, `12634` = 12634L, `12635` = 12635L, `12636` = 12636L, 
`12637` = 12637L, `12638` = 12638L, `12639` = 12639L, `12640` = 12640L, 
`12641` = 12641L, `12642` = 12642L, `12643` = 12643L, `12644` = 12644L, 
`12645` = 12645L, `12646` = 12646L, `12647` = 12647L, `12648` = 12648L, 
`12649` = 12649L, `12650` = 12650L, `12651` = 12651L, `12652` = 12652L, 
`12653` = 12653L, `12654` = 12654L, `12655` = 12655L, `12656` = 12656L, 
`12657` = 12657L, `12658` = 12658L, `12659` = 12659L, `12660` = 12660L, 
`12661` = 12661L, `12662` = 12662L, `12663` = 12663L, `12664` = 12664L, 
`12665` = 12665L, `12666` = 12666L, `12667` = 12667L, `12668` = 12668L, 
`12669` = 12669L, `12670` = 12670L, `12671` = 12671L, `12672` = 12672L, 
`12673` = 12673L, `12674` = 12674L, `12675` = 12675L, `12676` = 12676L, 
`12677` = 12677L, `12678` = 12678L, `12679` = 12679L, `12680` = 12680L, 
`12681` = 12681L, `12682` = 12682L, `12683` = 12683L, `12684` = 12684L, 
`12685` = 12685L, `12686` = 12686L, `12687` = 12687L, `12688` = 12688L, 
`12689` = 12689L, `12690` = 12690L, `12691` = 12691L, `12692` = 12692L, 
`12693` = 12693L, `12694` = 12694L, `12695` = 12695L, `12696` = 12696L, 
`12697` = 12697L, `12698` = 12698L, `12699` = 12699L, `12700` = 12700L, 
`12701` = 12701L, `12702` = 12702L, `12703` = 12703L, `12704` = 12704L, 
`12705` = 12705L, `12706` = 12706L, `12707` = 12707L, `12708` = 12708L, 
`12709` = 12709L, `12710` = 12710L, `12711` = 12711L, `12712` = 12712L, 
`12713` = 12713L, `12714` = 12714L, `12715` = 12715L, `12716` = 12716L, 
`12717` = 12717L, `12718` = 12718L, `12719` = 12719L, `12720` = 12720L, 
`12721` = 12721L, `12722` = 12722L, `12723` = 12723L, `12724` = 12724L, 
`12725` = 12725L, `12726` = 12726L, `12727` = 12727L, `12728` = 12728L, 
`12729` = 12729L, `12730` = 12730L, `12731` = 12731L, `12732` = 12732L, 
`12733` = 12733L, `12734` = 12734L, `12735` = 12735L, `12736` = 12736L, 
`12737` = 12737L, `12738` = 12738L, `12739` = 12739L, `12740` = 12740L, 
`12741` = 12741L, `12742` = 12742L, `12743` = 12743L, `12744` = 12744L, 
`12745` = 12745L, `12746` = 12746L, `12747` = 12747L, `12748` = 12748L, 
`12749` = 12749L, `12750` = 12750L, `12751` = 12751L, `12752` = 12752L, 
`12753` = 12753L, `12754` = 12754L, `12755` = 12755L, `12756` = 12756L, 
`12757` = 12757L, `12758` = 12758L, `12759` = 12759L, `12760` = 12760L, 
`12761` = 12761L, `12762` = 12762L, `12763` = 12763L, `12764` = 12764L, 
`12765` = 12765L, `12766` = 12766L, `12767` = 12767L, `12768` = 12768L, 
`12769` = 12769L, `12770` = 12770L, `12771` = 12771L, `12772` = 12772L, 
`12773` = 12773L, `12774` = 12774L, `12775` = 12775L, `12776` = 12776L, 
`12777` = 12777L, `12778` = 12778L, `12779` = 12779L, `12780` = 12780L, 
`12781` = 12781L, `12782` = 12782L, `12783` = 12783L, `12784` = 12784L, 
`12785` = 12785L, `12786` = 12786L, `12787` = 12787L, `12788` = 12788L, 
`12789` = 12789L, `12790` = 12790L, `12791` = 12791L, `12792` = 12792L, 
`12793` = 12793L, `12794` = 12794L, `12795` = 12795L, `12796` = 12796L, 
`12797` = 12797L, `12798` = 12798L, `12799` = 12799L, `12800` = 12800L, 
`12801` = 12801L, `12802` = 12802L, `12803` = 12803L, `12804` = 12804L, 
`12805` = 12805L, `12806` = 12806L, `12807` = 12807L, `12808` = 12808L, 
`12809` = 12809L, `12810` = 12810L, `12811` = 12811L, `12812` = 12812L, 
`12813` = 12813L, `12814` = 12814L, `12815` = 12815L, `12816` = 12816L, 
`12817` = 12817L, `12818` = 12818L, `12819` = 12819L, `12820` = 12820L, 
`12821` = 12821L, `12822` = 12822L, `12823` = 12823L, `12824` = 12824L, 
`12825` = 12825L, `12826` = 12826L, `12827` = 12827L, `12828` = 12828L, 
`12829` = 12829L, `12830` = 12830L, `12831` = 12831L, `12832` = 12832L, 
`12833` = 12833L, `12834` = 12834L, `12835` = 12835L, `12836` = 12836L, 
`12837` = 12837L, `12838` = 12838L, `12839` = 12839L, `12840` = 12840L, 
`12841` = 12841L, `12842` = 12842L, `12843` = 12843L, `12844` = 12844L, 
`12845` = 12845L, `12846` = 12846L, `12847` = 12847L, `12848` = 12848L, 
`12849` = 12849L, `12850` = 12850L, `12851` = 12851L, `12852` = 12852L, 
`12853` = 12853L, `12854` = 12854L, `12855` = 12855L, `12856` = 12856L, 
`12857` = 12857L, `12858` = 12858L, `12859` = 12859L, `12860` = 12860L, 
`12861` = 12861L, `12862` = 12862L, `12863` = 12863L, `12864` = 12864L, 
`12865` = 12865L, `12866` = 12866L, `12867` = 12867L, `12868` = 12868L, 
`12869` = 12869L, `12870` = 12870L, `12871` = 12871L, `12872` = 12872L, 
`12873` = 12873L, `12874` = 12874L, `12875` = 12875L, `12876` = 12876L, 
`12877` = 12877L, `12878` = 12878L, `12879` = 12879L, `12880` = 12880L, 
`12881` = 12881L, `12882` = 12882L, `12883` = 12883L, `12884` = 12884L, 
`12885` = 12885L, `12886` = 12886L, `12887` = 12887L, `12888` = 12888L, 
`12889` = 12889L, `12890` = 12890L, `12891` = 12891L, `12892` = 12892L, 
`12893` = 12893L, `12894` = 12894L, `12895` = 12895L, `12896` = 12896L, 
`12897` = 12897L, `12898` = 12898L, `12899` = 12899L, `12900` = 12900L, 
`12901` = 12901L, `12902` = 12902L, `12903` = 12903L, `12904` = 12904L, 
`12905` = 12905L, `12906` = 12906L, `12907` = 12907L, `12908` = 12908L, 
`12909` = 12909L, `12910` = 12910L, `12911` = 12911L, `12912` = 12912L, 
`12913` = 12913L, `12914` = 12914L, `12915` = 12915L, `12916` = 12916L, 
`12917` = 12917L, `12918` = 12918L, `12919` = 12919L, `12920` = 12920L, 
`12921` = 12921L, `12922` = 12922L, `12923` = 12923L, `12924` = 12924L, 
`12925` = 12925L, `12926` = 12926L, `12927` = 12927L, `12928` = 12928L, 
`12929` = 12929L, `12930` = 12930L, `12931` = 12931L, `12932` = 12932L, 
`12933` = 12933L, `12934` = 12934L, `12935` = 12935L, `12936` = 12936L, 
`12937` = 12937L, `12938` = 12938L, `12939` = 12939L, `12940` = 12940L, 
`12941` = 12941L, `12942` = 12942L, `12943` = 12943L, `12944` = 12944L, 
`12945` = 12945L, `12946` = 12946L, `12947` = 12947L, `12948` = 12948L, 
`12949` = 12949L, `12950` = 12950L, `12951` = 12951L, `12952` = 12952L, 
`12953` = 12953L, `12954` = 12954L, `12955` = 12955L, `12956` = 12956L, 
`12957` = 12957L, `12958` = 12958L, `12959` = 12959L, `12960` = 12960L, 
`12961` = 12961L, `12962` = 12962L, `12963` = 12963L, `12964` = 12964L, 
`12965` = 12965L, `12966` = 12966L, `12967` = 12967L, `12968` = 12968L, 
`12969` = 12969L, `12970` = 12970L, `12971` = 12971L, `12972` = 12972L, 
`12973` = 12973L, `12974` = 12974L, `12975` = 12975L, `12976` = 12976L, 
`12977` = 12977L, `12978` = 12978L, `12979` = 12979L, `12980` = 12980L, 
`12981` = 12981L, `12982` = 12982L, `12983` = 12983L, `12984` = 12984L, 
`12985` = 12985L, `12986` = 12986L, `12987` = 12987L, `12988` = 12988L, 
`12989` = 12989L, `12990` = 12990L, `12991` = 12991L, `12992` = 12992L, 
`12993` = 12993L, `12994` = 12994L, `12995` = 12995L, `12996` = 12996L, 
`12997` = 12997L, `12998` = 12998L, `12999` = 12999L, `13000` = 13000L, 
`13001` = 13001L, `13002` = 13002L, `13003` = 13003L, `13004` = 13004L, 
`13005` = 13005L, `13006` = 13006L, `13007` = 13007L, `13008` = 13008L, 
`13009` = 13009L, `13010` = 13010L, `13011` = 13011L, `13012` = 13012L, 
`13013` = 13013L, `13014` = 13014L, `13015` = 13015L, `13016` = 13016L, 
`13017` = 13017L, `13018` = 13018L, `13019` = 13019L, `13020` = 13020L, 
`13021` = 13021L, `13022` = 13022L, `13023` = 13023L, `13024` = 13024L, 
`13025` = 13025L, `13026` = 13026L, `13027` = 13027L, `13028` = 13028L, 
`13029` = 13029L, `13030` = 13030L, `13031` = 13031L, `13032` = 13032L, 
`13033` = 13033L, `13034` = 13034L, `13035` = 13035L, `13036` = 13036L, 
`13037` = 13037L, `13038` = 13038L, `13039` = 13039L, `13040` = 13040L, 
`13041` = 13041L, `13042` = 13042L, `13043` = 13043L, `13044` = 13044L, 
`13045` = 13045L, `13046` = 13046L, `13047` = 13047L, `13048` = 13048L, 
`13049` = 13049L, `13050` = 13050L, `13051` = 13051L, `13052` = 13052L, 
`13053` = 13053L, `13054` = 13054L, `13055` = 13055L, `13056` = 13056L, 
`13057` = 13057L, `13058` = 13058L, `13059` = 13059L, `13060` = 13060L, 
`13061` = 13061L, `13062` = 13062L, `13063` = 13063L, `13064` = 13064L, 
`13065` = 13065L, `13066` = 13066L, `13067` = 13067L, `13068` = 13068L, 
`13069` = 13069L, `13070` = 13070L, `13071` = 13071L, `13072` = 13072L, 
`13073` = 13073L, `13074` = 13074L, `13075` = 13075L, `13076` = 13076L, 
`13077` = 13077L, `13078` = 13078L, `13079` = 13079L, `13080` = 13080L, 
`13081` = 13081L, `13082` = 13082L, `13083` = 13083L, `13084` = 13084L, 
`13085` = 13085L, `13086` = 13086L, `13087` = 13087L, `13088` = 13088L, 
`13089` = 13089L, `13090` = 13090L, `13091` = 13091L, `13092` = 13092L, 
`13093` = 13093L, `13094` = 13094L, `13095` = 13095L, `13096` = 13096L, 
`13097` = 13097L, `13098` = 13098L, `13099` = 13099L, `13100` = 13100L, 
`13101` = 13101L, `13102` = 13102L, `13103` = 13103L, `13104` = 13104L, 
`13105` = 13105L, `13106` = 13106L, `13107` = 13107L, `13108` = 13108L, 
`13109` = 13109L, `13110` = 13110L, `13111` = 13111L, `13112` = 13112L, 
`13113` = 13113L, `13114` = 13114L, `13115` = 13115L, `13116` = 13116L, 
`13117` = 13117L, `13118` = 13118L, `13119` = 13119L, `13120` = 13120L, 
`13121` = 13121L, `13122` = 13122L, `13123` = 13123L, `13124` = 13124L, 
`13125` = 13125L, `13126` = 13126L, `13127` = 13127L, `13128` = 13128L, 
`13129` = 13129L, `13130` = 13130L, `13131` = 13131L, `13132` = 13132L, 
`13133` = 13133L, `13134` = 13134L, `13135` = 13135L, `13136` = 13136L, 
`13137` = 13137L, `13138` = 13138L, `13139` = 13139L, `13140` = 13140L, 
`13141` = 13141L, `13142` = 13142L, `13143` = 13143L, `13144` = 13144L, 
`13145` = 13145L, `13146` = 13146L, `13147` = 13147L, `13148` = 13148L, 
`13149` = 13149L, `13150` = 13150L, `13151` = 13151L, `13152` = 13152L, 
`13153` = 13153L, `13154` = 13154L, `13155` = 13155L, `13156` = 13156L, 
`13157` = 13157L, `13158` = 13158L, `13159` = 13159L, `13160` = 13160L, 
`13161` = 13161L, `13162` = 13162L, `13163` = 13163L, `13164` = 13164L, 
`13165` = 13165L, `13166` = 13166L, `13167` = 13167L, `13168` = 13168L, 
`13169` = 13169L, `13170` = 13170L, `13171` = 13171L, `13172` = 13172L, 
`13173` = 13173L, `13174` = 13174L, `13175` = 13175L, `13176` = 13176L, 
`13177` = 13177L, `13178` = 13178L, `13179` = 13179L, `13180` = 13180L, 
`13181` = 13181L, `13182` = 13182L, `13183` = 13183L, `13184` = 13184L, 
`13185` = 13185L, `13186` = 13186L, `13187` = 13187L, `13188` = 13188L, 
`13189` = 13189L, `13190` = 13190L, `13191` = 13191L, `13192` = 13192L, 
`13193` = 13193L, `13194` = 13194L, `13195` = 13195L, `13196` = 13196L, 
`13197` = 13197L, `13198` = 13198L, `13199` = 13199L, `13200` = 13200L, 
`13201` = 13201L, `13202` = 13202L, `13203` = 13203L, `13204` = 13204L, 
`13205` = 13205L, `13206` = 13206L, `13207` = 13207L, `13208` = 13208L, 
`13209` = 13209L, `13210` = 13210L, `13211` = 13211L, `13212` = 13212L, 
`13213` = 13213L, `13214` = 13214L, `13215` = 13215L, `13216` = 13216L, 
`13217` = 13217L, `13218` = 13218L, `13219` = 13219L, `13220` = 13220L, 
`13221` = 13221L, `13222` = 13222L, `13223` = 13223L, `13224` = 13224L, 
`13225` = 13225L, `13226` = 13226L, `13227` = 13227L, `13228` = 13228L, 
`13229` = 13229L, `13230` = 13230L, `13231` = 13231L, `13232` = 13232L, 
`13233` = 13233L, `13234` = 13234L, `13235` = 13235L, `13236` = 13236L, 
`13237` = 13237L, `13238` = 13238L, `13239` = 13239L, `13240` = 13240L, 
`13241` = 13241L, `13242` = 13242L, `13243` = 13243L, `13244` = 13244L, 
`13245` = 13245L, `13246` = 13246L, `13247` = 13247L, `13248` = 13248L, 
`13249` = 13249L, `13250` = 13250L, `13251` = 13251L, `13252` = 13252L, 
`13253` = 13253L, `13254` = 13254L, `13255` = 13255L, `13256` = 13256L, 
`13257` = 13257L, `13258` = 13258L, `13259` = 13259L, `13260` = 13260L, 
`13261` = 13261L, `13262` = 13262L, `13263` = 13263L, `13264` = 13264L, 
`13265` = 13265L, `13266` = 13266L, `13267` = 13267L, `13268` = 13268L, 
`13269` = 13269L, `13270` = 13270L, `13271` = 13271L, `13272` = 13272L, 
`13273` = 13273L, `13274` = 13274L, `13275` = 13275L, `13276` = 13276L, 
`13277` = 13277L, `13278` = 13278L, `13279` = 13279L, `13280` = 13280L, 
`13281` = 13281L, `13282` = 13282L, `13283` = 13283L, `13284` = 13284L, 
`13285` = 13285L, `13286` = 13286L, `13287` = 13287L, `13288` = 13288L, 
`13289` = 13289L, `13290` = 13290L, `13291` = 13291L, `13292` = 13292L, 
`13293` = 13293L, `13294` = 13294L, `13295` = 13295L, `13296` = 13296L, 
`13297` = 13297L, `13298` = 13298L, `13299` = 13299L, `13300` = 13300L, 
`13301` = 13301L, `13302` = 13302L, `13303` = 13303L, `13304` = 13304L, 
`13305` = 13305L, `13306` = 13306L, `13307` = 13307L, `13308` = 13308L, 
`13309` = 13309L, `13310` = 13310L, `13311` = 13311L, `13312` = 13312L, 
`13313` = 13313L, `13314` = 13314L, `13315` = 13315L, `13316` = 13316L, 
`13317` = 13317L, `13318` = 13318L, `13319` = 13319L, `13320` = 13320L, 
`13321` = 13321L, `13322` = 13322L, `13323` = 13323L, `13324` = 13324L, 
`13325` = 13325L, `13326` = 13326L, `13327` = 13327L, `13328` = 13328L, 
`13329` = 13329L, `13330` = 13330L, `13331` = 13331L, `13332` = 13332L, 
`13333` = 13333L, `13334` = 13334L, `13335` = 13335L, `13336` = 13336L, 
`13337` = 13337L, `13338` = 13338L, `13339` = 13339L, `13340` = 13340L, 
`13341` = 13341L, `13342` = 13342L, `13343` = 13343L, `13344` = 13344L, 
`13345` = 13345L, `13346` = 13346L, `13347` = 13347L, `13348` = 13348L, 
`13349` = 13349L, `13350` = 13350L, `13351` = 13351L, `13352` = 13352L, 
`13353` = 13353L, `13354` = 13354L, `13355` = 13355L, `13356` = 13356L, 
`13357` = 13357L, `13358` = 13358L, `13359` = 13359L, `13360` = 13360L, 
`13361` = 13361L, `13362` = 13362L, `13363` = 13363L, `13364` = 13364L, 
`13365` = 13365L, `13366` = 13366L, `13367` = 13367L, `13368` = 13368L, 
`13369` = 13369L, `13370` = 13370L, `13371` = 13371L, `13372` = 13372L, 
`13373` = 13373L, `13374` = 13374L, `13375` = 13375L, `13376` = 13376L, 
`13377` = 13377L, `13378` = 13378L, `13379` = 13379L, `13380` = 13380L, 
`13381` = 13381L, `13382` = 13382L, `13383` = 13383L, `13384` = 13384L, 
`13385` = 13385L, `13386` = 13386L, `13387` = 13387L, `13388` = 13388L, 
`13389` = 13389L, `13390` = 13390L, `13391` = 13391L, `13392` = 13392L, 
`13393` = 13393L, `13394` = 13394L, `13395` = 13395L, `13396` = 13396L, 
`13397` = 13397L, `13398` = 13398L, `13399` = 13399L, `13400` = 13400L, 
`13401` = 13401L, `13402` = 13402L, `13403` = 13403L, `13404` = 13404L, 
`13405` = 13405L, `13406` = 13406L, `13407` = 13407L, `13408` = 13408L, 
`13409` = 13409L, `13410` = 13410L, `13411` = 13411L, `13412` = 13412L, 
`13413` = 13413L, `13414` = 13414L, `13415` = 13415L, `13416` = 13416L, 
`13417` = 13417L, `13418` = 13418L, `13419` = 13419L, `13420` = 13420L, 
`13421` = 13421L, `13422` = 13422L, `13423` = 13423L, `13424` = 13424L, 
`13425` = 13425L, `13426` = 13426L, `13427` = 13427L, `13428` = 13428L, 
`13429` = 13429L, `13430` = 13430L, `13431` = 13431L, `13432` = 13432L, 
`13433` = 13433L, `13434` = 13434L, `13435` = 13435L, `13436` = 13436L, 
`13437` = 13437L, `13438` = 13438L, `13439` = 13439L, `13440` = 13440L, 
`13441` = 13441L, `13442` = 13442L, `13443` = 13443L, `13444` = 13444L, 
`13445` = 13445L, `13446` = 13446L, `13447` = 13447L, `13448` = 13448L, 
`13449` = 13449L, `13450` = 13450L, `13451` = 13451L, `13452` = 13452L, 
`13453` = 13453L, `13454` = 13454L, `13455` = 13455L, `13456` = 13456L, 
`13457` = 13457L, `13458` = 13458L, `13459` = 13459L, `13460` = 13460L, 
`13461` = 13461L, `13462` = 13462L, `13463` = 13463L, `13464` = 13464L, 
`13465` = 13465L, `13466` = 13466L, `13467` = 13467L, `13468` = 13468L, 
`13469` = 13469L, `13470` = 13470L, `13471` = 13471L, `13472` = 13472L, 
`13473` = 13473L, `13474` = 13474L, `13475` = 13475L, `13476` = 13476L, 
`13477` = 13477L, `13478` = 13478L, `13479` = 13479L, `13480` = 13480L, 
`13481` = 13481L, `13482` = 13482L, `13483` = 13483L, `13484` = 13484L, 
`13485` = 13485L, `13486` = 13486L, `13487` = 13487L, `13488` = 13488L, 
`13489` = 13489L, `13490` = 13490L, `13491` = 13491L, `13492` = 13492L, 
`13493` = 13493L, `13494` = 13494L, `13495` = 13495L, `13496` = 13496L, 
`13497` = 13497L, `13498` = 13498L, `13499` = 13499L, `13500` = 13500L, 
`13501` = 13501L, `13502` = 13502L, `13503` = 13503L, `13504` = 13504L, 
`13505` = 13505L, `13506` = 13506L, `13507` = 13507L, `13508` = 13508L, 
`13509` = 13509L, `13510` = 13510L, `13511` = 13511L, `13512` = 13512L, 
`13513` = 13513L, `13514` = 13514L, `13515` = 13515L, `13516` = 13516L, 
`13517` = 13517L, `13518` = 13518L, `13519` = 13519L, `13520` = 13520L, 
`13521` = 13521L, `13522` = 13522L, `13523` = 13523L, `13524` = 13524L, 
`13525` = 13525L, `13526` = 13526L, `13527` = 13527L, `13528` = 13528L, 
`13529` = 13529L, `13530` = 13530L, `13531` = 13531L, `13532` = 13532L, 
`13533` = 13533L, `13534` = 13534L, `13535` = 13535L, `13536` = 13536L, 
`13537` = 13537L, `13538` = 13538L, `13539` = 13539L, `13540` = 13540L, 
`13541` = 13541L, `13542` = 13542L, `13543` = 13543L, `13544` = 13544L, 
`13545` = 13545L, `13546` = 13546L, `13547` = 13547L, `13548` = 13548L, 
`13549` = 13549L), class = "omit"), row.names = 73:172, class = "data.frame")

If it can be useful, by doing
head(London)
this is what came out

date area average_price      code houses_sold no_of_crimes borough_flag
73 2001-01-01    7          <NA> E09000001          24            0            1
74 2001-02-01    7          <NA> E09000001          37            0            1
75 2001-03-01    7          <NA> E09000001          44            0            1
76 2001-04-01    7          <NA> E09000001          38            0            1
77 2001-05-01    7          <NA> E09000001          30            0            1
78 2001-06-01    7          <NA> E09000001          36            0            1
> 

Well yes and no.

What you did was correct but you missed some of the text at the top of the output.

Here is a tiny example f dput() output

xx = 1:10, yy = c("a", "b", "c", "d", "e", "f", 
"g", "h", "i", "j")), class = "data.frame", row.names = c(NA, 
-10L))

So you are missing

structure(list(

etc, etc.

What you gave us is the part of one variable. You need to make sure that you get everything from

structure(list(

to

class = "data.frame", row.names = c(NA, 
-10L))

Don'n worry about the -10L, Your number will be different

Just for practice, try

dput(head(London,  20))

It should make copying the output easier and may give us enough to start with.

Thank You,
I tried with

dput(head(London,  20))

and this is what I got

`13030` = 13030L, `13031` = 13031L, `13032` = 13032L, `13033` = 13033L, 
`13034` = 13034L, `13035` = 13035L, `13036` = 13036L, `13037` = 13037L, 
`13038` = 13038L, `13039` = 13039L, `13040` = 13040L, `13041` = 13041L, 
`13042` = 13042L, `13043` = 13043L, `13044` = 13044L, `13045` = 13045L, 
`13046` = 13046L, `13047` = 13047L, `13048` = 13048L, `13049` = 13049L, 
`13050` = 13050L, `13051` = 13051L, `13052` = 13052L, `13053` = 13053L, 
`13054` = 13054L, `13055` = 13055L, `13056` = 13056L, `13057` = 13057L, 
`13058` = 13058L, `13059` = 13059L, `13060` = 13060L, `13061` = 13061L, 
`13062` = 13062L, `13063` = 13063L, `13064` = 13064L, `13065` = 13065L, 
`13066` = 13066L, `13067` = 13067L, `13068` = 13068L, `13069` = 13069L, 
`13070` = 13070L, `13071` = 13071L, `13072` = 13072L, `13073` = 13073L, 
`13074` = 13074L, `13075` = 13075L, `13076` = 13076L, `13077` = 13077L, 
`13078` = 13078L, `13079` = 13079L, `13080` = 13080L, `13081` = 13081L, 
`13082` = 13082L, `13083` = 13083L, `13084` = 13084L, `13085` = 13085L, 
`13086` = 13086L, `13087` = 13087L, `13088` = 13088L, `13089` = 13089L, 
`13090` = 13090L, `13091` = 13091L, `13092` = 13092L, `13093` = 13093L, 
`13094` = 13094L, `13095` = 13095L, `13096` = 13096L, `13097` = 13097L, 
`13098` = 13098L, `13099` = 13099L, `13100` = 13100L, `13101` = 13101L, 
`13102` = 13102L, `13103` = 13103L, `13104` = 13104L, `13105` = 13105L, 
`13106` = 13106L, `13107` = 13107L, `13108` = 13108L, `13109` = 13109L, 
`13110` = 13110L, `13111` = 13111L, `13112` = 13112L, `13113` = 13113L, 
`13114` = 13114L, `13115` = 13115L, `13116` = 13116L, `13117` = 13117L, 
`13118` = 13118L, `13119` = 13119L, `13120` = 13120L, `13121` = 13121L, 
`13122` = 13122L, `13123` = 13123L, `13124` = 13124L, `13125` = 13125L, 
`13126` = 13126L, `13127` = 13127L, `13128` = 13128L, `13129` = 13129L, 
`13130` = 13130L, `13131` = 13131L, `13132` = 13132L, `13133` = 13133L, 
`13134` = 13134L, `13135` = 13135L, `13136` = 13136L, `13137` = 13137L, 
`13138` = 13138L, `13139` = 13139L, `13140` = 13140L, `13141` = 13141L, 
`13142` = 13142L, `13143` = 13143L, `13144` = 13144L, `13145` = 13145L, 
`13146` = 13146L, `13147` = 13147L, `13148` = 13148L, `13149` = 13149L, 
`13150` = 13150L, `13151` = 13151L, `13152` = 13152L, `13153` = 13153L, 
`13154` = 13154L, `13155` = 13155L, `13156` = 13156L, `13157` = 13157L, 
`13158` = 13158L, `13159` = 13159L, `13160` = 13160L, `13161` = 13161L, 
`13162` = 13162L, `13163` = 13163L, `13164` = 13164L, `13165` = 13165L, 
`13166` = 13166L, `13167` = 13167L, `13168` = 13168L, `13169` = 13169L, 
`13170` = 13170L, `13171` = 13171L, `13172` = 13172L, `13173` = 13173L, 
`13174` = 13174L, `13175` = 13175L, `13176` = 13176L, `13177` = 13177L, 
`13178` = 13178L, `13179` = 13179L, `13180` = 13180L, `13181` = 13181L, 
`13182` = 13182L, `13183` = 13183L, `13184` = 13184L, `13185` = 13185L, 
`13186` = 13186L, `13187` = 13187L, `13188` = 13188L, `13189` = 13189L, 
`13190` = 13190L, `13191` = 13191L, `13192` = 13192L, `13193` = 13193L, 
`13194` = 13194L, `13195` = 13195L, `13196` = 13196L, `13197` = 13197L, 
`13198` = 13198L, `13199` = 13199L, `13200` = 13200L, `13201` = 13201L, 
`13202` = 13202L, `13203` = 13203L, `13204` = 13204L, `13205` = 13205L, 
`13206` = 13206L, `13207` = 13207L, `13208` = 13208L, `13209` = 13209L, 
`13210` = 13210L, `13211` = 13211L, `13212` = 13212L, `13213` = 13213L, 
`13214` = 13214L, `13215` = 13215L, `13216` = 13216L, `13217` = 13217L, 
`13218` = 13218L, `13219` = 13219L, `13220` = 13220L, `13221` = 13221L, 
`13222` = 13222L, `13223` = 13223L, `13224` = 13224L, `13225` = 13225L, 
`13226` = 13226L, `13227` = 13227L, `13228` = 13228L, `13229` = 13229L, 
`13230` = 13230L, `13231` = 13231L, `13232` = 13232L, `13233` = 13233L, 
`13234` = 13234L, `13235` = 13235L, `13236` = 13236L, `13237` = 13237L, 
`13238` = 13238L, `13239` = 13239L, `13240` = 13240L, `13241` = 13241L, 
`13242` = 13242L, `13243` = 13243L, `13244` = 13244L, `13245` = 13245L, 
`13246` = 13246L, `13247` = 13247L, `13248` = 13248L, `13249` = 13249L, 
`13250` = 13250L, `13251` = 13251L, `13252` = 13252L, `13253` = 13253L, 
`13254` = 13254L, `13255` = 13255L, `13256` = 13256L, `13257` = 13257L, 
`13258` = 13258L, `13259` = 13259L, `13260` = 13260L, `13261` = 13261L, 
`13262` = 13262L, `13263` = 13263L, `13264` = 13264L, `13265` = 13265L, 
`13266` = 13266L, `13267` = 13267L, `13268` = 13268L, `13269` = 13269L, 
`13270` = 13270L, `13271` = 13271L, `13272` = 13272L, `13273` = 13273L, 
`13274` = 13274L, `13275` = 13275L, `13276` = 13276L, `13277` = 13277L, 
`13278` = 13278L, `13279` = 13279L, `13280` = 13280L, `13281` = 13281L, 
`13282` = 13282L, `13283` = 13283L, `13284` = 13284L, `13285` = 13285L, 
`13286` = 13286L, `13287` = 13287L, `13288` = 13288L, `13289` = 13289L, 
`13290` = 13290L, `13291` = 13291L, `13292` = 13292L, `13293` = 13293L, 
`13294` = 13294L, `13295` = 13295L, `13296` = 13296L, `13297` = 13297L, 
`13298` = 13298L, `13299` = 13299L, `13300` = 13300L, `13301` = 13301L, 
`13302` = 13302L, `13303` = 13303L, `13304` = 13304L, `13305` = 13305L, 
`13306` = 13306L, `13307` = 13307L, `13308` = 13308L, `13309` = 13309L, 
`13310` = 13310L, `13311` = 13311L, `13312` = 13312L, `13313` = 13313L, 
`13314` = 13314L, `13315` = 13315L, `13316` = 13316L, `13317` = 13317L, 
`13318` = 13318L, `13319` = 13319L, `13320` = 13320L, `13321` = 13321L, 
`13322` = 13322L, `13323` = 13323L, `13324` = 13324L, `13325` = 13325L, 
`13326` = 13326L, `13327` = 13327L, `13328` = 13328L, `13329` = 13329L, 
`13330` = 13330L, `13331` = 13331L, `13332` = 13332L, `13333` = 13333L, 
`13334` = 13334L, `13335` = 13335L, `13336` = 13336L, `13337` = 13337L, 
`13338` = 13338L, `13339` = 13339L, `13340` = 13340L, `13341` = 13341L, 
`13342` = 13342L, `13343` = 13343L, `13344` = 13344L, `13345` = 13345L, 
`13346` = 13346L, `13347` = 13347L, `13348` = 13348L, `13349` = 13349L, 
`13350` = 13350L, `13351` = 13351L, `13352` = 13352L, `13353` = 13353L, 
`13354` = 13354L, `13355` = 13355L, `13356` = 13356L, `13357` = 13357L, 
`13358` = 13358L, `13359` = 13359L, `13360` = 13360L, `13361` = 13361L, 
`13362` = 13362L, `13363` = 13363L, `13364` = 13364L, `13365` = 13365L, 
`13366` = 13366L, `13367` = 13367L, `13368` = 13368L, `13369` = 13369L, 
`13370` = 13370L, `13371` = 13371L, `13372` = 13372L, `13373` = 13373L, 
`13374` = 13374L, `13375` = 13375L, `13376` = 13376L, `13377` = 13377L, 
`13378` = 13378L, `13379` = 13379L, `13380` = 13380L, `13381` = 13381L, 
`13382` = 13382L, `13383` = 13383L, `13384` = 13384L, `13385` = 13385L, 
`13386` = 13386L, `13387` = 13387L, `13388` = 13388L, `13389` = 13389L, 
`13390` = 13390L, `13391` = 13391L, `13392` = 13392L, `13393` = 13393L, 
`13394` = 13394L, `13395` = 13395L, `13396` = 13396L, `13397` = 13397L, 
`13398` = 13398L, `13399` = 13399L, `13400` = 13400L, `13401` = 13401L, 
`13402` = 13402L, `13403` = 13403L, `13404` = 13404L, `13405` = 13405L, 
`13406` = 13406L, `13407` = 13407L, `13408` = 13408L, `13409` = 13409L, 
`13410` = 13410L, `13411` = 13411L, `13412` = 13412L, `13413` = 13413L, 
`13414` = 13414L, `13415` = 13415L, `13416` = 13416L, `13417` = 13417L, 
`13418` = 13418L, `13419` = 13419L, `13420` = 13420L, `13421` = 13421L, 
`13422` = 13422L, `13423` = 13423L, `13424` = 13424L, `13425` = 13425L, 
`13426` = 13426L, `13427` = 13427L, `13428` = 13428L, `13429` = 13429L, 
`13430` = 13430L, `13431` = 13431L, `13432` = 13432L, `13433` = 13433L, 
`13434` = 13434L, `13435` = 13435L, `13436` = 13436L, `13437` = 13437L, 
`13438` = 13438L, `13439` = 13439L, `13440` = 13440L, `13441` = 13441L, 
`13442` = 13442L, `13443` = 13443L, `13444` = 13444L, `13445` = 13445L, 
`13446` = 13446L, `13447` = 13447L, `13448` = 13448L, `13449` = 13449L, 
`13450` = 13450L, `13451` = 13451L, `13452` = 13452L, `13453` = 13453L, 
`13454` = 13454L, `13455` = 13455L, `13456` = 13456L, `13457` = 13457L, 
`13458` = 13458L, `13459` = 13459L, `13460` = 13460L, `13461` = 13461L, 
`13462` = 13462L, `13463` = 13463L, `13464` = 13464L, `13465` = 13465L, 
`13466` = 13466L, `13467` = 13467L, `13468` = 13468L, `13469` = 13469L, 
`13470` = 13470L, `13471` = 13471L, `13472` = 13472L, `13473` = 13473L, 
`13474` = 13474L, `13475` = 13475L, `13476` = 13476L, `13477` = 13477L, 
`13478` = 13478L, `13479` = 13479L, `13480` = 13480L, `13481` = 13481L, 
`13482` = 13482L, `13483` = 13483L, `13484` = 13484L, `13485` = 13485L, 
`13486` = 13486L, `13487` = 13487L, `13488` = 13488L, `13489` = 13489L, 
`13490` = 13490L, `13491` = 13491L, `13492` = 13492L, `13493` = 13493L, 
`13494` = 13494L, `13495` = 13495L, `13496` = 13496L, `13497` = 13497L, 
`13498` = 13498L, `13499` = 13499L, `13500` = 13500L, `13501` = 13501L, 
`13502` = 13502L, `13503` = 13503L, `13504` = 13504L, `13505` = 13505L, 
`13506` = 13506L, `13507` = 13507L, `13508` = 13508L, `13509` = 13509L, 
`13510` = 13510L, `13511` = 13511L, `13512` = 13512L, `13513` = 13513L, 
`13514` = 13514L, `13515` = 13515L, `13516` = 13516L, `13517` = 13517L, 
`13518` = 13518L, `13519` = 13519L, `13520` = 13520L, `13521` = 13521L, 
`13522` = 13522L, `13523` = 13523L, `13524` = 13524L, `13525` = 13525L, 
`13526` = 13526L, `13527` = 13527L, `13528` = 13528L, `13529` = 13529L, 
`13530` = 13530L, `13531` = 13531L, `13532` = 13532L, `13533` = 13533L, 
`13534` = 13534L, `13535` = 13535L, `13536` = 13536L, `13537` = 13537L, 
`13538` = 13538L, `13539` = 13539L, `13540` = 13540L, `13541` = 13541L, 
`13542` = 13542L, `13543` = 13543L, `13544` = 13544L, `13545` = 13545L, 
`13546` = 13546L, `13547` = 13547L, `13548` = 13548L, `13549` = 13549L
), class = "omit"), row.names = 73:92, class = "data.frame")

I also tried with

dput(head(London, 20)[c("average_price", "area", "houses_sold", "no_of_crimes")])

and I got

"97", "98", "99", "100", "101", "102", "103", "104", "105", "106", 
"107", "108", "109", "110", "111", "112", "113", "114", "115", 
"116", "117", "118", "119", "120", "121", "122", "123", "124", 
"125", "126", "127", "128", "129", "130", "131", "132", "133", 
"134", "135", "136", "137", "138", "139", "140", "141", "142", 
"143", "144", "145", "146", "147", "148", "149", "150", "151", 
"152", "153", "154", "155", "156", "157", "158", "159", "160", 
"161", "162", "163", "164", "165", "166", "167", "168", "169", 
"170", "171", "172", "173", "174", "175", "176", "177", "178", 
"179", "180", "181", "182", "183", "184", "185", "186", "187", 
"188", "189", "190", "191", "192", "193", "194", "195", "196", 
"197", "198", "199", "200", "201", "202", "203", "204", "205", 
"206", "207", "208", "209", "210", "211", "212", "213", "214", 
"215", "216", "217", "218", "219", "220", "221", "222", "223", 
"224", "225", "226", "227", "228", "229", "230", "231", "232", 
"233", "234", "235", "236", "237", "238", "239", "240", "241", 
"242", "243", "244", "245", "246", "247", "248", "249", "250", 
"251", "252", "253", "254", "255", "256", "257", "258", "259", 
"260", "261", "262", "263", "264", "265", "266", "267", "268", 
"269", "270", "271", "272", "273", "274", "275", "276", "277", 
"278", "279", "280", "281", "282", "283", "284", "285", "286", 
"287", "288", "289", "290", "291", "292", "293", "294", "295", 
"296", "297", "298", "299", "300", "301", "302", "303", "304", 
"305", "306", "307", "308", "309", "310", "311", "312", "313", 
"314", "315", "316", "317", "318", "319", "320", "321", "322", 
"323", "324", "325", "326", "327", "328", "329", "330", "331", 
"332", "333", "334", "335", "336", "337", "338", "339", "340", 
"341", "342", "343", "344", "345", "346", "347", "348", "349", 
"350", "351", "352", "353", "354", "355", "356", "357", "358", 
"359", "360", "361", "362", "363", "364", "365", "366", "367", 
"368", "369", "370", "371", "372", "373", "374", "375", "376", 
"377", "378", "379", "380", "381", "382", "383", "384", "385", 
"386", "387", "388", "389", "390", "391", "392", "393", "394", 
"395", "396", "397", "398", "399", "400", "401", "402", "403", 
"404", "405", "406", "407", "408", "409", "410", "411", "412", 
"413", "414", "415", "416", "417", "418", "419", "420", "421", 
"422", "423", "424", "425", "426", "427", "428", "429", "430", 
"431", "432", "433", "434", "435", "436", "437", "438", "439", 
"440", "441", "442", "443", "444", "445", "446", "447", "448", 
"449", "450", "451", "452", "453", "454", "455", "456", "457", 
"458", "459", "460", "461", "462", "463", "464", "465", "466", 
"467", "468", "469", "470", "471", "472", "473", "474", "475", 
"476", "477", "478", "479", "480", "481", "482", "483", "484", 
"485", "486", "487", "488", "489", "490", "491", "492", "493", 
"494", "495", "496", "497", "498", "499", "500", "501", "502", 
"503", "504", "505", "506", "507", "508", "509", "510", "511", 
"512", "513", "514", "515", "516", "517", "518", "519", "520", 
"521", "522", "523", "524", "525", "526", "527", "528", "529", 
"530", "531", "532", "533", "534", "535", "536", "537", "538", 
"539", "540", "541", "542", "543", "544", "545", "546", "547", 
"548", "549", "550", "551", "552", "553", "554", "555", "556", 
"557", "558", "559", "560", "561", "562", "563", "564", "565", 
"566", "567", "568", "569", "570", "571", "572", "573", "574", 
"575", "576", "577", "578", "579", "581", "582", "583", "584", 
"585", "586", "587", "588", "589", "591", "592", "593", "594", 
"595", "597", "598", "599", "600", "601", "602", "603", "604", 
"605", "607", "608", "609", "610", "611", "612", "613", "615", 
"616", "617", "618", "619", "620", "621", "623", "624", "625", 
"626", "627", "628", "629", "630", "631", "633", "634", "636", 
"637", "638", "639", "640", "641", "642", "643", "644", "645", 
"646", "647", "649", "653", "655", "656", "657", "658", "659", 
"660", "661", "662", "663", "664", "665", "666", "667", "668", 
"671", "672", "674", "678", "679", "680", "681", "682", "684", 
"685", "688", "689", "692", "693", "694", "695", "696", "697", 
"698", "699", "700", "701", "702", "703", "704", "705", "707", 
"708", "711", "713", "714", "715", "716", "719", "723", "725", 
"727", "729", "730", "733", "735", "736", "737", "738", "739", 
"741", "743", "745", "748", "750", "753", "754", "755", "756", 
"757", "760", "761", "763", "764", "771", "774", "785", "786", 
"788", "789", "791", "792", "793", "794", "798", "799", "806", 
"807", "812", "816", "817", "820", "824", "828", "830", "832", 
"841", "847", "851", "852", "856", "870", "876", "884", "886", 
"888", "891", "892", "894", "896", "914", "916", "935", "951", 
"963"), class = "factor"), no_of_crimes = c(0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = 73:92, class = "data.frame")

Oh, I may have made a stupid assumption. Generally we expect a data.frame or tibble but you may not be working with one.

What does

str(London)

and

class(London)

give us?

Can you also give us your code?

Again, just copy and paste between
```

```

str(London)

gives me this

'data.frame':	7375 obs. of  7 variables:
 $ date         : chr  "2001-01-01" "2001-02-01" "2001-03-01" "2001-04-01" ...
 $ area         : int  7 7 7 7 7 7 7 7 7 7 ...
 $ average_price: Factor w/ 7312 levels "82343","83266",..: 3365 987 785 1180 1745 2329 2006 2029 1980 2924 ...
 $ code         : chr  "E09000001" "E09000001" "E09000001" "E09000001" ...
 $ houses_sold  : Factor w/ 729 levels "2","5","6","8",..: 19 31 35 32 25 30 33 25 25 21 ...
 $ no_of_crimes : num  0 0 0 0 0 0 0 0 0 0 ...
 $ borough_flag : int  1 1 1 1 1 1 1 1 1 1 ...
 - attr(*, "na.action")= 'omit' Named int [1:6174] 1 2 3 4 5 6 7 8 9 10 ...
  ..- attr(*, "names")= chr [1:6174] "1" "2" "3" "4" ...

while

class(London)

gives me this

[1] "data.frame"

Okay so we do have a data.frame. Then, I cannot see why you are getting the results you are.

Try this and see what you get:

dat <- data.frame(xx = 1:10, yy = letters[1:10])

dput(dat)

Is that data set publically available? If so can you give us a link?

thank you for your reply.

This is what I get

structure(list(xx = 1:10, yy = c("a", "b", "c", "d", "e", "f", 
"g", "h", "i", "j")), class = "data.frame", row.names = c(NA, 
-10L))

yes, the dataset is publically available, you can find it here Housing in London | Kaggle

Okay, so dput() is working properly.

I 've got the file, now we need your code. Just copy and past it here between
```

```

as with the data.

In the mean time I'm off to breakfast so it will be a little while before I get back to you.

my code:

# Upload the dataset
London <- read.csv("housing_in_london_monthly_variables.csv", header = TRUE, sep = ',')
str(London)
dim(London)
head(London)
summary(London)

# dataset cleaning
London <- na.omit(London[!is.na(London$area), ])
London <- na.omit(London[!is.na(London$houses_sold), ])
London <- na.omit(London[!is.na(London$no_of_crimes), ])

# Print unique values of text features

# AVERAGE PRICE
unique(London$average_price)
London$average_price <- as.factor(London$average_price)
print(levels(London$average_price))

# AREA
unique(London$area)
London$area <- as.factor(London$area)
print(levels(London$area))

# HOUSES SOLD
unique(London$houses_sold)
London$houses_sold <- as.factor(London$houses_sold)
print(levels(London$houses_sold))

# NUMBER OF CRIMES
unique(London$no_of_crimes)
str(London$no_of_crimes)

#We change the categorical variables to numerical variables.
London$area <- as.integer(London$area)

# Create features and target matrixes
library(dplyr)
X <- London %>% 
  select(houses_sold, area, no_of_crimes)
y <- London$average_price

# Scale data
library(caret)
preprocessParams <- preProcess(cbind(X, y), method = c("center", "scale"))
scaled_data <- predict(preprocessParams, cbind(X, y))

# Splitting training set into two parts based on outcome: 75% and 25%
index <- createDataPartition(y, p=0.75, list=FALSE)
X_train <- X[ index, ]
X_test <- X[-index, ]
y_train <- y[index]
y_test<-y[-index]

when I run

index <- createDataPartition(y, p=0.75, list=FALSE)

the error I talked about comes in.

In createDataPartition(y, p = 0.75, list = FALSE) :
  Some classes have a single record ( 1003535, 1007474, 1013362, 1013417, 1017286, 1020823, 1023157, 1024447, 1025448, 1025837, 1029585, 1030332, 1030803, 1032875, 1034779, 1035583, 1039759, 1040211, 1040700, 1042777, 1043141, 1044834, 1058206, 1058953, 1060664, 1060810, 1060969, 1067550, 1071778, 1073991, 1076419, 1077366, 1086593, 1088417, 1096072, 1098770, 1109054, 1116111, 1117408, 1122471, 1154073, 1159559, 1168770, 1172514, 1189245, 1191209, 1193336, 1198222, 1204771, 1206813, 1209578, 1217676, 1217729, 1218687, 1226681, 1229599, 1231111, 1237349, 1246352, 1246384, 1257578, 1259725, 1262257, 1263468, 1264909, 1266556, 1268780, 1270090, 1278113, 1282708, 1285331, 1287616, 1288940, 1291645, 1293028, 1294113, 1294603, 1295078, 1295310, 1297755, 1301729, 1303324, 1304186, 1306192, 1306556, 1306557, 1313357, 1316116, 1319174, 1319380, 1325081, 1328276, 132897, 132999, 133060, 133329, 1334423, 1334876, 1335351, 133857, 134022, 134081, 1341276, 134249, 134327, 1344311, 1344464, 134457, 13 [...]

My next goal would be to create and fit Ridge and Lasso objects. But when I run this code:

# Create and fit Lasso and Ridge objects
lasso<-train(y= y_train,
             x = X_train,
             method = 'glmnet', 
             tuneGrid = expand.grid(alpha = 1, lambda = 1)
             
) 

this pops up

Something is wrong; all the Accuracy metric values are missing:
    Accuracy       Kappa    
 Min.   : NA   Min.   : NA  
 1st Qu.: NA   1st Qu.: NA  
 Median : NA   Median : NA  
 Mean   :NaN   Mean   :NaN  
 3rd Qu.: NA   3rd Qu.: NA  
 Max.   : NA   Max.   : NA  
 NA's   :1     NA's   :1    
Error: Stopping

> warnings()

1: model fit failed for Resample01: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

2: model fit failed for Resample02: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

3: model fit failed for Resample03: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

4: model fit failed for Resample04: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

5: model fit failed for Resample05: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

6: model fit failed for Resample06: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

7: model fit failed for Resample07: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

8: model fit failed for Resample08: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

9: model fit failed for Resample09: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

10: model fit failed for Resample10: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

11: model fit failed for Resample11: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

12: model fit failed for Resample12: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

13: model fit failed for Resample13: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

14: model fit failed for Resample14: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

15: model fit failed for Resample15: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

16: model fit failed for Resample16: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

17: model fit failed for Resample17: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

18: model fit failed for Resample18: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

19: model fit failed for Resample19: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

20: model fit failed for Resample20: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

21: model fit failed for Resample21: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

22: model fit failed for Resample22: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

23: model fit failed for Resample23: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

24: model fit failed for Resample24: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

25: model fit failed for Resample25: alpha=1, lambda=1 Error in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

26: In nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo,  ... :
  There were missing values in resampled performance measures.

Thank you

Okay, got it. I know nothing about {caret} but I may be able to do something.

BTW dput() is working okay for me

dput(head(DTM, 20))
structure(list(date = structure(c(9131L, 9162L, 9190L, 9221L, 
9251L, 9282L, 9312L, 9343L, 9374L, 9404L, 9435L, 9465L, 9496L, 
9527L, 9556L, 9587L, 9617L, 9648L, 9678L, 9709L), class = c("IDate", 
"Date")), area = c("city of london", "city of london", "city of london", 
"city of london", "city of london", "city of london", "city of london", 
"city of london", "city of london", "city of london", "city of london", 
"city of london", "city of london", "city of london", "city of london", 
"city of london", "city of london", "city of london", "city of london", 
"city of london"), average_price = c(91449L, 82203L, 79121L, 
77101L, 84409L, 94901L, 110128L, 112329L, 104473L, 108038L, 117636L, 
127232L, 108999L, 93357L, 93707L, 120543L, 112050L, 114226L, 
97547L, 114179L), code = c("E09000001", "E09000001", "E09000001", 
"E09000001", "E09000001", "E09000001", "E09000001", "E09000001", 
"E09000001", "E09000001", "E09000001", "E09000001", "E09000001", 
"E09000001", "E09000001", "E09000001", "E09000001", "E09000001", 
"E09000001", "E09000001"), houses_sold = c(17L, 7L, 14L, 7L, 
10L, 17L, 13L, 14L, 17L, 14L, 11L, 18L, 17L, 10L, 17L, 14L, 19L, 
28L, 30L, 25L), no_of_crimes = c(NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_), borough_flag = c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
)), row.names = c(NA, -20L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x55daef8ccf60>)

I'm sorry, what is DTM?

Sorry, I keep a few canned scripts handy for doing thing like checking data or data cleaning. DTM is just a copy of London so that I do not have to keep typing London.

Okay I think I found the problems.

London$average_price <- as.factor(London$average_price)
London$houses_sold <- as.factor(London$houses_sold)

Both of these variables were already numeric plus you forgot to change them back to numeric.

This seems to work.

suppressMessages(library(tidyverse))
suppressMessages(library(caret))

London <- read.csv("housing_in_london_monthly_variables.csv")
# dataset cleaning
London <- na.omit(London[!is.na(London$area), ]) # Not needed
London <- na.omit(London[!is.na(London$houses_sold), ])
London <- na.omit(London[!is.na(London$no_of_crimes), ]) # 45 % of the data

London$area <- as.factor(London$area)
London$area <- as.integer(London$area)

# Create features and target matrixes
X <- London %>% 
  select(houses_sold, area, no_of_crimes)
y <- London$average_price

# Scale data
library(caret)
preprocessParams <- preProcess(cbind(X, y), method = c("center", "scale"))
scaled_data <- predict(preprocessParams, cbind(X, y))

# Splitting training set into two parts based on outcome: 75% and 25%
index <- createDataPartition(y, p=0.75, list=FALSE)
X_train <- X[ index, ]
X_test <- X[-index, ]
y_train <- y[index]
y_test<-y[-index]

index <- createDataPartition(y, p=0.75, list=FALSE)
1 Like

It works! thank you!

I'm sorry, another question. I changed my dataset, it's from the same source as before, but now it's "housing_in_london_yearly_variables.csv", not "montly". I'm stuck here:

data.frame(
  ridge = as.data.frame.matrix(coef(ridge$finalModel, ridge$finalModel$lambdaOpt)),
  lasso = as.data.frame.matrix(coef(lasso$finalModel, lasso$finalModel$lambdaOpt)), 
  linear = (linear$finalModel$coefficients)
) %>%   rename(lasso = X1, ridge = X1.1)

it says to me

Error in data.frame(ridge = as.data.frame.matrix(coef(ridge$finalModel,  : 
 arguments imply differing number of rows: 6, 0

this is my new data

# Upload the dataset
London <- read.csv("housing_in_london_yearly_variables.csv", header = TRUE, sep = ',')
str(London)
dim(London)
head(London)
summary(London)

# Print unique values of text features

# MEDIAN SALARY
unique(London$median_salary)
London$median_salary <- as.integer(London$median_salary)
print(levels(London$median_salary))

# AREA
unique(London$area)
London$area <- as.factor(London$area)
print(levels(London$area))

# LIFE SATISFACTION
unique(London$life_satisfaction)
London$life_satisfaction <- as.factor(London$life_satisfaction)
print(levels(London$life_satisfaction))
London$life_satisfaction <- as.numeric(London$life_satisfaction)

# POPULATION SIZE
unique(London$population_size)
London$population_size <- as.factor(London$population_size)
print(levels(London$population_size))

# NUMBER OF JOBS
unique(London$number_of_jobs)
London$number_of_jobs <- as.factor(London$number_of_jobs)
print(levels(London$number_of_jobs))
London$number_of_jobs <- as.integer(London$number_of_jobs)

# NUMBER OF HOUSES
unique(London$no_of_houses)
London$no_of_houses <- as.integer(London$no_of_houses)
print(levels(London$no_of_houses))

#We change the categorical variables to numerical variables.
London$area <- as.integer(London$area)
head(London)
summary(London$area)
(London$area)
str(London$area)

# Create features and target matrixes
library(dplyr)
X <- London %>% 
  select(life_satisfaction, area, population_size, no_of_houses, median_salary)
y <- London$number_of_jobs

# Scale data
library(caret)
preprocessParams <- preProcess(cbind(X, y), method = c("center", "scale"))
scaled_data <- predict(preprocessParams, cbind(X, y))

# Splitting training set into two parts based on outcome: 75% and 25%
index <- createDataPartition(y, p=0.75, list=FALSE)
X_train <- X[ index, ]
X_test <- X[-index, ]
y_train <- y[index]
y_test<-y[-index]

# Create and fit Lasso and Ridge objects
lasso<-train(y= y_train,
             x = X_train,
             method = 'glmnet', 
             tuneGrid = expand.grid(alpha = 1, lambda = 1)
             
) 

ridge<-train(y = y_train,
             x = X_train,
             method = 'glmnet', 
             tuneGrid = expand.grid(alpha = 0, lambda = 1)
             
) 

# Make the predictions using Lasso and Ridge
predictions_lasso <- lasso %>% predict(X_test)
predictions_ridge <- ridge %>% predict(X_test)

# Print R squared scores
data.frame(
  Ridge_R2 = R2(predictions_ridge, y_test),
  Lasso_R2 = R2(predictions_lasso, y_test)
)

# Ridge_R2  Lasso_R2
# 0.8040862 0.8022575

# Print RMSE
data.frame(
  Ridge_RMSE = RMSE(predictions_ridge, y_test) , 
  Lasso_RMSE = RMSE(predictions_lasso, y_test) 
)

# Ridge_RMSE Lasso_RMSE
# 48.0953   48.14168

# Print coefficients
data.frame(
  as.data.frame.matrix(coef(lasso$finalModel, lasso$bestTune$lambda)),
  as.data.frame.matrix(coef(ridge$finalModel, ridge$bestTune$lambda))
)%>%
  rename(Lasso_coef = s1, Ridge_coef = s1.1)

#Ridge has a better prediction performance

#Regularization parameter
parameters <- c(seq(0.1, 2, by =0.1) ,  seq(2, 5, 0.5) , seq(5, 25, 1))

lasso<-train(y = y_train,
             x = X_train,
             method = 'glmnet', 
             tuneGrid = expand.grid(alpha = 1, lambda = parameters) ,
             metric =  "Rsquared"
) 

ridge<-train(y = y_train,
             x = X_train,
             method = 'glmnet', 
             tuneGrid = expand.grid(alpha = 0, lambda = parameters),
             metric =  "Rsquared"
             
) 
linear<-train(y = y_train, 
              x = X_train, 
              method = 'glmnet',
              metric =  "Rsquared"
)

print(paste0('Lasso best parameters: ' , lasso$finalModel$lambdaOpt))
## [1] "Lasso best parameters: 2.5"
print(paste0('Ridge best parameters: ' , ridge$finalModel$lambdaOpt))
## [1] "Ridge best parameters: 25"
predictions_lasso <- lasso %>% predict(X_test)
predictions_ridge <- ridge %>% predict(X_test)
predictions_lin <- linear %>% predict(X_test)

data.frame(
  Ridge_R2 = R2(predictions_ridge, y_test),
  Lasso_R2 = R2(predictions_lasso, y_test),
  Linear_R2 = R2(predictions_lin, y_test)

##Ridge_R2  Lasso_R2 Linear_R2
## 0.8040862 0.7885605 0.8018532

data.frame(
  Ridge_RMSE = RMSE(predictions_ridge, y_test) , 
  Lasso_RMSE = RMSE(predictions_lasso, y_test) , 
  Linear_RMSE = RMSE(predictions_ridge, y_test)
)

##Ridge_RMSE Lasso_RMSE Linear_RMSE
## 48.0953   52.10062     48.0953

print('Best estimator coefficients')
## [1] "Best estimator coefficients"

data.frame(
  ridge = as.data.frame.matrix(coef(ridge$finalModel, ridge$finalModel$lambdaOpt)),
  lasso = as.data.frame.matrix(coef(lasso$finalModel, lasso$finalModel$lambdaOpt)), 
  linear = (linear$finalModel$coefficients)
) %>%   rename(lasso = X1, ridge = X1.1)

what's the problem here?
Thank you

Got it.
I'll have a quick look now but I have to go out soon so it may be a while before I can get back to you.

I see the problem . What are you trying to achieve in creating this data frame?

As it stands

data.frame(
 ridge = as.data.frame.matrix(coef(ridge$finalModel, ridge$finalModel$lambdaOpt)),
 lasso = as.data.frame.matrix(coef(lasso$finalModel, lasso$finalModel$lambdaOpt)), 
 linear = (linear$finalModel)
) %>%   rename(lasso = X1, ridge = X1.1)

is impossible. You are trying to put two wildly different things with different dimensions together. Well linear gives us a dim of NULL.

class(ridge) ; ridge
dim(ridge)
class(lasso) ; lasso
dim(lasso)
class(linear) ; linear
dim(linear)

You can do this

data.frame(ridge,lasso)

Anyway here is a slightly tidied up version of your code.

suppressMessages(library(tidyverse))
suppressMessages(library(caret))
London <- read.csv("housing_in_london_yearly_variables.csv", header = TRUE, sep = ',')

# Remove NAs====
London <- na.omit(London[!is.na(London$life_satisfaction), ])
London <- na.omit(London[!is.na(London$area_size), ])
London <- na.omit(London[!is.na(London$no_of_houses), ])
London <- na.omit(London[!is.na(London$number_of_jobs), ])
London <- na.omit(London[!is.na(London$population_size), ])
London <- na.omit(London[!is.na(London$median_salary), ])

# Data transformation ====
London$area <- as.factor(London$area)
London$area <- as.integer(London$area)

# Create features and target matrixes ====
X <- London %>% 
  select(life_satisfaction, area, population_size, no_of_houses, median_salary)
y <- London$number_of_jobs

# Scale data ====
preprocessParams <- preProcess(cbind(X, y), method = c("center", "scale"))
scaled_data <- predict(preprocessParams, cbind(X, y))

# Splitting training set into two parts based on outcome: 75% and 25% ====
index <- createDataPartition(y, p=0.75, list=FALSE)
X_train <- X[ index, ]
X_test <- X[-index, ]
y_train <- y[index]
y_test<-y[-index]

# Create and fit Lasso and Ridge objects====
lasso<-train(y= y_train,
             x = X_train,
             method = 'glmnet', 
             tuneGrid = expand.grid(alpha = 1, lambda = 1)
             
) 

ridge<-train(y = y_train,
             x = X_train,
             method = 'glmnet', 
             tuneGrid = expand.grid(alpha = 0, lambda = 1)
             
) 

# Make the predictions using Lasso and Ridge====
predictions_lasso <- lasso %>% predict(X_test)
predictions_ridge <- ridge %>% predict(X_test)

# Print R squared scores ====
data.frame(
  Ridge_R2 = R2(predictions_ridge, y_test),
  Lasso_R2 = R2(predictions_lasso, y_test)
)

# Print RMSE====
data.frame(
  Ridge_RMSE = RMSE(predictions_ridge, y_test) , 
  Lasso_RMSE = RMSE(predictions_lasso, y_test) 
)

# Print coefficients====
data.frame(
  as.data.frame.matrix(coef(lasso$finalModel, lasso$bestTune$lambda)),
  as.data.frame.matrix(coef(ridge$finalModel, ridge$bestTune$lambda))
)%>%
  rename(Lasso_coef = s1, Ridge_coef = s1.1)

# Regularization parameter One ====
parameters <- c(seq(0.1, 2, by =0.1) ,  seq(2, 5, 0.5) , seq(5, 25, 1))

lasso<-train(y = y_train,
             x = X_train,
             method = 'glmnet', 
             tuneGrid = expand.grid(alpha = 1, lambda = parameters) ,
             metric =  "Rsquared"
) 

ridge<-train(y = y_train,
             x = X_train,
             method = 'glmnet', 
             tuneGrid = expand.grid(alpha = 0, lambda = parameters),
             metric =  "Rsquared"
             
) 
linear<-train(y = y_train, 
              x = X_train, 
              method = 'glmnet',
              metric =  "Rsquared"
)

print(paste0('Lasso best parameters: ' , lasso$finalModel$lambdaOpt))
print(paste0('Ridge best parameters: ' , ridge$finalModel$lambdaOpt))

# Predictions ====
predictions_lasso <- lasso %>% predict(X_test)
predictions_ridge <- ridge %>% predict(X_test)
predictions_lin <- linear %>% predict(X_test)

# Ridge_R2 ====
data.frame(
  Ridge_R2 = R2(predictions_ridge, y_test),
  Lasso_R2 = R2(predictions_lasso, y_test),
  Linear_R2 = R2(predictions_lin, y_test)
)

# Ridge_RMSE ====
data.frame(
  Ridge_RMSE = RMSE(predictions_ridge, y_test) , 
  Lasso_RMSE = RMSE(predictions_lasso, y_test) , 
  Linear_RMSE = RMSE(predictions_ridge, y_test)
)

print('Best estimator coefficients')

# Problem ====
data.frame(
  ridge = as.data.frame.matrix(coef(ridge$finalModel, ridge$finalModel$lambdaOpt)),
  lasso = as.data.frame.matrix(coef(lasso$finalModel, lasso$finalModel$lambdaOpt)), 
  linear = (linear$finalModel)
) %>%   rename(lasso = X1, ridge = X1.1)