In R, how can I count the number of variable after argmax? - r

this below is sample of raw data which I have:
sentiment
pos neu neg
1 0 0
0.2 0.3 0.5
0.3 0.3 0.4
0 0 1
0.2 0.7 0.1
In this raw data of sentiment, "pos" refers to probability of positivity in comment, "neu" refers to probability of neutrality and "neg" refers to probability of negativity. I want to choose the highest probability among pos, neu and neg. and know the number of each sentiment. For example, pos : 0.6, neu : 0.2, neg : 0.2 is positive comment.
My desired output is like below:
pos_count neu_count neg_count
1 1 3
Could you help me make this?
Below is also dput :
structure(list(likes_count = c(0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L,
0L, 0L, 0L, 3L, 1L, 2L, 2L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
2L, 5L, 2L, 0L, 1L, 0L), neg = c(0, 0.41, 0, 0, 0,
0.19, 0, 1, 0, 0.52, 0, 0.11, 0.02, 0.05, 0.02, 0, 0, 0.01, 0.01,
0, 0, 0.97, 0, 0.01, 0.24, 0.34, 0.94, 0.44, 0.15, 0.01), neu = c(0,
0.1, 0, 0, 0, 0, 0.13, 0, 0.98, 0.32, 0, 0.08, 0.02, 0.04, 0.07,
0, 0, 0.98, 0.07, 0, 0, 0.03, 0.02, 0.21, 0.48, 0.62, 0.01, 0.2,
0.85, 0.67), pos = c(1, 0.48, 1, 1, 1, 0.81, 0.86,
0, 0.02, 0.16, 1, 0.81, 0.96, 0.91, 0.91, 1, 1, 0.01, 0.92, 1,
1, 0, 0.98, 0.78, 0.28, 0.04, 0.05, 0.36, 0, 0.32)), na.action = structure(c(`7` = 7L,
`11` = 11L, `38` = 38L, `53` = 53L, `88` = 88L, `101` = 101L,
`106` = 106L, `138` = 138L, `139` = 139L, `155` = 155L, `165` = 165L,
`176` = 176L, `178` = 178L, `179` = 179L, `199` = 199L, `200` = 200L,
`201` = 201L, `208` = 208L, `209` = 209L, `250` = 250L, `281` = 281L,
`293` = 293L, `299` = 299L, `316` = 316L, `321` = 321L, `322` = 322L,
`328` = 328L, `332` = 332L, `333` = 333L, `334` = 334L, `335` = 335L,
`336` = 336L, `342` = 342L, `347` = 347L, `352` = 352L, `354` = 354L,
`355` = 355L, `395` = 395L, `398` = 398L, `400` = 400L, `411` = 411L,
`420` = 420L, `449` = 449L, `454` = 454L, `456` = 456L, `457` = 457L,
`464` = 464L, `471` = 471L, `491` = 491L, `495` = 495L, `502` = 502L,
`503` = 503L, `504` = 504L, `506` = 506L, `526` = 526L, `536` = 536L,
`541` = 541L, `542` = 542L, `546` = 546L, `556` = 556L, `558` = 558L,
`563` = 563L, `579` = 579L, `581` = 581L, `582` = 582L, `584` = 584L,
`602` = 602L, `603` = 603L, `604` = 604L, `606` = 606L, `614` = 614L,
`617` = 617L, `619` = 619L, `620` = 620L, `621` = 621L, `622` = 622L,
`623` = 623L, `625` = 625L, `626` = 626L, `629` = 629L, `630` = 630L,
`631` = 631L, `632` = 632L, `633` = 633L, `636` = 636L, `637` = 637L,
`638` = 638L, `639` = 639L, `640` = 640L, `643` = 643L, `645` = 645L,
`646` = 646L, `647` = 647L, `648` = 648L, `650` = 650L, `652` = 652L,
`653` = 653L, `655` = 655L, `656` = 656L, `658` = 658L, `661` = 661L,
`665` = 665L, `666` = 666L, `667` = 667L, `669` = 669L, `671` = 671L,
`673` = 673L, `674` = 674L, `679` = 679L, `680` = 680L, `682` = 682L,
`683` = 683L, `684` = 684L, `685` = 685L, `686` = 686L, `687` = 687L,
`689` = 689L, `692` = 692L, `694` = 694L, `696` = 696L, `697` = 697L,
`699` = 699L, `700` = 700L, `701` = 701L, `702` = 702L, `703` = 703L,
`704` = 704L, `705` = 705L, `707` = 707L, `708` = 708L, `712` = 712L,
`713` = 713L, `714` = 714L, `717` = 717L, `718` = 718L, `719` = 719L,
`720` = 720L, `721` = 721L, `722` = 722L, `723` = 723L, `724` = 724L,
`725` = 725L, `726` = 726L, `727` = 727L, `728` = 728L, `730` = 730L,
`738` = 738L, `750` = 750L, `753` = 753L, `754` = 754L, `761` = 761L,
`766` = 766L, `767` = 767L, `769` = 769L, `771` = 771L, `775` = 775L,
`786` = 786L, `808` = 808L, `810` = 810L, `812` = 812L, `814` = 814L,
`817` = 817L, `820` = 820L, `841` = 841L, `862` = 862L, `864` = 864L,
`865` = 865L, `866` = 866L, `867` = 867L, `874` = 874L, `877` = 877L,
`878` = 878L, `881` = 881L, `882` = 882L, `890` = 890L, `891` = 891L,
`913` = 913L, `934` = 934L, `938` = 938L, `951` = 951L, `961` = 961L,
`962` = 962L, `967` = 967L, `971` = 971L, `972` = 972L, `981` = 981L,
`983` = 983L, `986` = 986L, `988` = 988L, `1000` = 1000L, `1014` = 1014L
), class = "omit"), row.names = c(NA, -30L), class = "data.frame")

Here is a base R solution.
It gets the index to the (first) maximum value on each row and then tables those values, assigning this table the same names as the data set's names.
s <- apply(sentiment[-1], 1, which.max)
res <- setNames(table(s), names(sentiment)[-1])
res
#neg neu pos
# 5 6 19
If there are ties, for instance, if two values have the same probability 0.5, use the following.
sent2 <- sentiment # work with a copy
sent2[8, c(2, 4)] <- 0.5 # assign 0.5 to 8th row, cols 2, 4
s2 <- apply(sent2[-1], 1, function(x) which(x == max(x)))
res2 <- setNames(table(unlist(s2)), names(sent2)[-1])
res2
#neg neu pos
# 5 6 20

Related

How to create a new data.table object in for loop based off an existing data.table and update columns?

I am trying emulate the following code
compile_1 <- compile_0[, final_1 := ifelse((!is.na(final_0) & is.na(input_1)), final_0, final_1)]
compile_2 <- compile_1[, final_2 := ifelse((!is.na(final_1) & is.na(input_2)), final_1, final_2)]
compile_3 <- compile_2[, final_3 := ifelse((!is.na(final_2) & is.na(input_3)), final_2, final_3)]
in a for loop but I am running issues, which I think (but I'm not entirely sure) is because i isn't being enclosed in the function's (assign) private environment see Enclosing variables within for loop.
My initial attempt at the for loop was
for(i in 1:3){
assign(paste0("compile_", get("i")-1), get(paste0("compile_", get("i",)-1))[, paste0("final_", get("i")) := ifelse((!is.na(get(paste0("final_", get("i")-1))) & is.na(get(paste0("input_",get("i"))))), get(paste0("final_", get("i")-1)),get(paste0("final_", get("i"))))])
}
I then tried adding a copy of i to the local environment as in this post, but this didn't work either, because it is then looking for compile_0 which is not in the function's local environment... i think?
dput(head(compile_0[, ..yy],20)) #i have removed some unecessary columns
structure(list(id = c("100", "10000", "10002", "10006", "1000794",
"10010", "1002162", "1002820", "1003005", "1003330", "1003458",
"1003617", "1004034", "1004235", "1004649", "1004731", "1004795",
"1004796", "1004798", "1004799"), final_0 = c(445L, 960L, 655L,
590L, 550L, 780L, 570L, 200L, 272L, 540L, 550L, 470L, 588L, 320L,
750L, 500L, 418L, 418L, 438L, 648L), final_1 = c(445L, 960L,
655L, 590L, 550L, 780L, 570L, 200L, 272L, 540L, 550L, 470L, 588L,
320L, 750L, 500L, 418L, 418L, 438L, 648L), final_2 = c(460L,
960L, NA, NA, NA, 780L, 570L, 200L, 200L, 540L, 550L, NA, 568L,
320L, 705L, 400L, 449L, 449L, 469L, 648L), final_3 = c(460L,
960L, NA, NA, NA, 780L, 570L, 200L, 200L, 540L, 550L, NA, 568L,
320L, 705L, 400L, 449L, 449L, 469L, 648L), input_0 = c(445L,
960L, 655L, 590L, 550L, 780L, 570L, 200L, 272L, 540L, 550L, 470L,
588L, 320L, 750L, 500L, 418L, 418L, 438L, 648L), input_1 = c(445L,
960L, 655L, 590L, 550L, 780L, 570L, 200L, 272L, 540L, 550L, 470L,
588L, 320L, 750L, 500L, 418L, 418L, 438L, 648L), input_2 = c(460L,
960L, NA, NA, NA, 780L, 570L, 200L, 200L, 540L, 550L, NA, 568L,
320L, 705L, 400L, 449L, 449L, 469L, 648L), input_3 = c(460L,
960L, NA, NA, NA, 780L, 570L, 200L, 200L, 540L, 550L, NA, 568L,
320L, 705L, 400L, 449L, 449L, 469L, 648L)), row.names = c(NA,
-20L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00000176df6d1ef0>, sorted = "id")
Thanks!
I was expecting the for loop to give the same output as the individual lines of code.
You have to use eval() when creating or editing columns in data.table.
require(data.table)
for(i in 1:3){
compile_old <- paste0('compile_',i-1)
compile_new <- paste0('compile_',i)
final_first <- paste0('final_',i-1)
final_second <- paste0('final_',i)
input <- paste0('input_',i)
assign(compile_new,
get(compile_old)[,eval(final_second):=ifelse((!is.na(get(final_first) & is.na(get(input)))), get(final_first), get(final_second))])
}
Furthermore, You need to keep in mind that after the for loop, compile_0, compile_1, compile_2, and compile_3 would be the same because of := in data.table.
So, you can keep the difference among them with copy() for deep copy.
for(i in 1:3){
compile_old <- paste0('compile_',i-1)
compile_new <- paste0('compile_',i)
final_first <- paste0('final_',i-1)
final_second <- paste0('final_',i)
input <- paste0('input_',i)
# copy() for deep copy
compile_old_copy <- copy(get(compile_old))
assign(compile_new,
compile_old_copy[,eval(final_second):=ifelse((!is.na(get(final_first) & is.na(get(input)))), get(final_first), get(final_second))])
}
You can see that all the compile_{} are different.

ggplot2: legend symbols matching plot symbols

I made a ggplot where I make use of the viridis color package. I adjusted the geom_point in the graph to different symbols. The symbols are not displaying in my legend, although the colors are correctly programmed. How can I match my legend (including symbols and colors) with my ggplot?
Attempt:
library(ggplot)
library(viridis)
ggplot(df, aes(`Lengte_(cm)`, verschil_lengte))+
geom_point(aes(shape = Lengteklasse, colour = Lengteklasse), size = 3)+
geom_hline(yintercept = 1.0, linetype="dashed", color = "red")+
geom_hline(yintercept = 2.0, linetype="dashed", color = "red")+
scale_shape_manual(values = c(16, 17, 15, 3, 8), guide = "none")+
scale_color_viridis(discrete = T, option = "D")+
scale_x_continuous(breaks = seq(7,12, by = 0.5))+
scale_y_continuous(breaks = seq(0,3, by = 0.5))+
labs(x = "Lengte (cm)", y = "Verschaling (mm)")+
guides(col = guide_legend("Lengteklasse (cm)"))+
theme_classic()
current outcome:
df =
structure(list(`Lengte_(cm)` = c(9, 10.7, 10.7, 7.7, 9.1, 11.2,
9.7, 10.2, 8.6, 8.9, 11.2, 11.4, 10.5, 10.5, 11.1, 8.9, 11.5,
10.4, 9.1, 9.2, 10.1, 7.8, 9.8, 8.2, 10.1, 10.5, 10.2, 7.9, 9.3,
8, 8.7, 8.9, 8.8, 9.3, 8.5, 7.7, 11.2, 9.4, 9.7, 11.2, 11, 10.7,
9), Lengteklasse = structure(c(4L, 5L, 5L, 2L, 4L, 6L, 4L, 5L,
3L, 3L, 6L, 6L, 5L, 5L, 6L, 3L, 6L, 5L, 4L, 4L, 5L, 2L, 4L, 3L,
5L, 5L, 5L, 2L, 4L, 3L, 3L, 3L, 3L, 4L, 3L, 2L, 6L, 4L, 4L, 6L,
6L, 5L, 4L), .Label = c("6", "7", "8", "9", "10", "11", "12",
"13"), class = "factor"), verschil_lengte = c(0, 1.4, 1.8, 1.8,
1.4, 0.800000000000001, 0.600000000000001, 0.600000000000001,
1.4, 1.9, 1.3, 1.5, 0.300000000000001, 0.5, 0.9, 2.2, 1, 1, 1.4,
2.1, 1.3, 2.2, 0.899999999999999, 2.3, 1.1, 0.699999999999999,
2.1, 0.4, 0.5, 0.9, 2.1, 1.6, 1.7, 0.799999999999999, 2, 2.1,
0.5, 0.799999999999999, 1.3, 0.4, 0.300000000000001, 1.6, 0.199999999999999
)), row.names = c(NA, -43L), class = c("tbl_df", "tbl", "data.frame"
), na.action = structure(c(`1` = 1L, `2` = 2L, `3` = 3L, `4` = 4L,
`5` = 5L, `6` = 6L, `7` = 7L, `8` = 8L, `9` = 9L, `10` = 10L,
`11` = 11L, `12` = 12L, `13` = 13L, `14` = 14L, `15` = 15L, `16` = 16L,
`17` = 17L, `18` = 18L, `19` = 19L, `20` = 20L, `21` = 21L, `22` = 22L,
`23` = 23L, `24` = 24L, `25` = 25L, `26` = 26L, `27` = 27L, `28` = 28L,
`29` = 29L, `30` = 30L, `31` = 31L, `32` = 32L, `33` = 33L, `34` = 34L,
`35` = 35L, `36` = 36L, `37` = 37L, `38` = 38L, `39` = 39L, `40` = 40L,
`41` = 41L, `42` = 42L, `43` = 43L, `44` = 44L, `45` = 45L, `46` = 46L,
`47` = 47L, `48` = 48L, `49` = 49L, `50` = 50L, `51` = 51L, `52` = 52L,
`53` = 53L, `54` = 54L, `55` = 55L, `56` = 56L, `57` = 57L, `58` = 58L,
`59` = 59L, `60` = 60L, `61` = 61L, `62` = 62L, `63` = 63L, `64` = 64L,
`65` = 65L, `66` = 66L, `67` = 67L, `68` = 68L, `69` = 69L, `70` = 70L,
`71` = 71L, `72` = 72L, `73` = 73L, `74` = 74L, `75` = 75L, `76` = 76L,
`77` = 77L, `78` = 78L, `79` = 79L, `80` = 80L, `81` = 81L, `82` = 82L,
`83` = 83L, `84` = 84L, `85` = 85L, `86` = 86L, `87` = 87L, `88` = 88L,
`89` = 89L, `90` = 90L, `91` = 91L, `92` = 92L, `93` = 93L, `94` = 94L,
`95` = 95L, `96` = 96L, `97` = 97L, `98` = 98L, `99` = 99L, `100` = 100L,
`101` = 101L, `102` = 102L, `103` = 103L, `104` = 104L, `105` = 105L,
`106` = 106L, `107` = 107L, `108` = 108L, `109` = 109L, `110` = 110L,
`111` = 111L, `112` = 112L, `113` = 113L, `114` = 114L, `115` = 115L,
`116` = 116L, `117` = 117L, `118` = 118L, `119` = 119L, `120` = 120L,
`121` = 121L, `122` = 122L, `123` = 123L, `124` = 124L, `125` = 125L,
`126` = 126L, `127` = 127L, `128` = 128L, `129` = 129L, `130` = 130L,
`131` = 131L, `132` = 132L, `133` = 133L, `134` = 134L, `135` = 135L,
`136` = 136L, `137` = 137L, `138` = 138L, `139` = 139L, `140` = 140L,
`141` = 141L, `142` = 142L, `143` = 143L, `144` = 144L, `145` = 145L,
`146` = 146L, `147` = 147L, `148` = 148L, `149` = 149L, `150` = 150L,
`151` = 151L, `152` = 152L, `153` = 153L, `154` = 154L, `155` = 155L,
`156` = 156L, `157` = 157L, `158` = 158L, `159` = 159L, `160` = 160L,
`161` = 161L, `162` = 162L, `163` = 163L, `164` = 164L, `165` = 165L,
`166` = 166L, `167` = 167L, `168` = 168L, `169` = 169L, `170` = 170L,
`171` = 171L, `172` = 172L, `173` = 173L, `174` = 174L, `175` = 175L,
`176` = 176L, `177` = 177L, `178` = 178L, `179` = 179L, `180` = 180L,
`181` = 181L, `182` = 182L, `183` = 183L, `184` = 184L, `185` = 185L,
`186` = 186L, `187` = 187L, `188` = 188L, `189` = 189L, `190` = 190L,
`191` = 191L, `192` = 192L, `193` = 193L, `194` = 194L, `195` = 195L,
`196` = 196L, `197` = 197L, `198` = 198L, `199` = 199L, `200` = 200L,
`201` = 201L, `202` = 202L, `203` = 203L, `204` = 204L, `205` = 205L,
`206` = 206L, `207` = 207L, `208` = 208L, `209` = 209L, `210` = 210L,
`211` = 211L, `212` = 212L, `213` = 213L, `214` = 214L, `215` = 215L,
`216` = 216L, `217` = 217L, `218` = 218L, `219` = 219L, `220` = 220L,
`221` = 221L, `222` = 222L, `223` = 223L, `224` = 224L, `225` = 225L,
`226` = 226L, `227` = 227L, `228` = 228L, `229` = 229L, `230` = 230L,
`231` = 231L, `232` = 232L, `233` = 233L, `234` = 234L, `235` = 235L,
`236` = 236L, `237` = 237L, `238` = 238L, `239` = 239L, `240` = 240L,
`241` = 241L, `242` = 242L, `243` = 243L, `244` = 244L, `245` = 245L,
`246` = 246L, `247` = 247L, `248` = 248L, `249` = 249L, `250` = 250L,
`251` = 251L, `252` = 252L, `253` = 253L, `254` = 254L, `255` = 255L,
`256` = 256L, `257` = 257L, `258` = 258L, `259` = 259L, `260` = 260L,
`261` = 261L, `262` = 262L, `263` = 263L, `264` = 264L, `265` = 265L,
`266` = 266L, `267` = 267L, `268` = 268L, `269` = 269L, `270` = 270L,
`271` = 271L, `272` = 272L, `273` = 273L, `274` = 274L, `275` = 275L,
`277` = 277L, `278` = 278L, `279` = 279L, `280` = 280L, `281` = 281L,
`282` = 282L, `284` = 284L, `285` = 285L, `286` = 286L, `288` = 288L,
`289` = 289L, `290` = 290L, `291` = 291L, `292` = 292L, `293` = 293L,
`294` = 294L, `295` = 295L, `296` = 296L, `297` = 297L, `298` = 298L,
`300` = 300L, `301` = 301L, `302` = 302L, `303` = 303L, `304` = 304L,
`305` = 305L, `306` = 306L, `308` = 308L, `309` = 309L, `310` = 310L,
`311` = 311L, `312` = 312L, `313` = 313L, `314` = 314L, `315` = 315L,
`316` = 316L, `317` = 317L, `318` = 318L, `319` = 319L, `321` = 321L,
`322` = 322L, `323` = 323L, `324` = 324L, `325` = 325L, `326` = 326L,
`327` = 327L, `328` = 328L, `329` = 329L, `330` = 330L, `331` = 331L,
`333` = 333L, `334` = 334L, `335` = 335L, `336` = 336L, `337` = 337L,
`338` = 338L, `339` = 339L, `340` = 340L, `341` = 341L, `342` = 342L,
`343` = 343L, `344` = 344L, `345` = 345L, `346` = 346L, `347` = 347L,
`348` = 348L, `349` = 349L, `351` = 351L, `352` = 352L, `354` = 354L,
`356` = 356L, `357` = 357L, `358` = 358L, `359` = 359L, `360` = 360L,
`361` = 361L, `362` = 362L, `363` = 363L, `364` = 364L, `366` = 366L,
`368` = 368L, `369` = 369L, `370` = 370L, `371` = 371L, `372` = 372L,
`373` = 373L, `374` = 374L, `375` = 375L, `376` = 376L, `377` = 377L,
`378` = 378L, `379` = 379L, `380` = 380L, `382` = 382L, `383` = 383L,
`384` = 384L, `387` = 387L, `388` = 388L, `390` = 390L, `391` = 391L,
`392` = 392L, `393` = 393L, `394` = 394L, `395` = 395L, `396` = 396L,
`397` = 397L, `399` = 399L, `400` = 400L, `401` = 401L, `402` = 402L,
`404` = 404L, `405` = 405L, `406` = 406L, `407` = 407L, `408` = 408L,
`409` = 409L, `410` = 410L, `411` = 411L, `412` = 412L, `413` = 413L,
`414` = 414L, `415` = 415L, `416` = 416L, `417` = 417L, `419` = 419L,
`420` = 420L, `423` = 423L, `424` = 424L, `425` = 425L, `426` = 426L,
`427` = 427L, `429` = 429L, `430` = 430L, `431` = 431L, `432` = 432L,
`433` = 433L, `434` = 434L, `435` = 435L, `436` = 436L, `437` = 437L,
`438` = 438L, `439` = 439L, `440` = 440L, `441` = 441L, `442` = 442L,
`443` = 443L, `444` = 444L, `446` = 446L, `447` = 447L, `448` = 448L,
`450` = 450L, `451` = 451L, `452` = 452L, `453` = 453L, `454` = 454L,
`455` = 455L, `456` = 456L, `457` = 457L, `459` = 459L, `460` = 460L,
`462` = 462L, `463` = 463L, `464` = 464L, `465` = 465L, `466` = 466L,
`467` = 467L, `468` = 468L, `469` = 469L, `470` = 470L, `471` = 471L,
`472` = 472L, `473` = 473L, `474` = 474L, `475` = 475L, `476` = 476L,
`478` = 478L, `479` = 479L, `480` = 480L, `481` = 481L, `482` = 482L,
`483` = 483L, `484` = 484L, `485` = 485L, `486` = 486L, `487` = 487L,
`488` = 488L, `489` = 489L, `490` = 490L, `491` = 491L, `493` = 493L,
`495` = 495L, `496` = 496L, `497` = 497L, `498` = 498L, `499` = 499L,
`500` = 500L, `501` = 501L, `502` = 502L, `503` = 503L, `504` = 504L,
`505` = 505L, `506` = 506L, `507` = 507L, `508` = 508L, `509` = 509L,
`510` = 510L, `511` = 511L, `512` = 512L, `513` = 513L, `514` = 514L,
`515` = 515L, `516` = 516L, `517` = 517L, `518` = 518L, `519` = 519L,
`520` = 520L, `521` = 521L, `522` = 522L, `523` = 523L, `524` = 524L,
`525` = 525L, `526` = 526L, `527` = 527L, `528` = 528L, `529` = 529L,
`530` = 530L, `531` = 531L, `532` = 532L, `533` = 533L, `535` = 535L,
`536` = 536L, `537` = 537L, `538` = 538L, `539` = 539L, `540` = 540L,
`542` = 542L, `543` = 543L, `544` = 544L, `545` = 545L, `546` = 546L,
`547` = 547L, `548` = 548L, `549` = 549L, `550` = 550L, `551` = 551L,
`553` = 553L, `554` = 554L, `555` = 555L, `556` = 556L, `557` = 557L,
`558` = 558L, `559` = 559L, `560` = 560L, `561` = 561L, `562` = 562L,
`563` = 563L, `564` = 564L, `565` = 565L, `566` = 566L, `567` = 567L,
`568` = 568L, `569` = 569L, `570` = 570L, `571` = 571L, `572` = 572L,
`573` = 573L, `574` = 574L, `575` = 575L, `576` = 576L, `577` = 577L,
`578` = 578L, `579` = 579L, `580` = 580L, `581` = 581L, `582` = 582L,
`583` = 583L, `584` = 584L, `585` = 585L, `586` = 586L, `587` = 587L,
`588` = 588L, `589` = 589L, `590` = 590L, `591` = 591L, `593` = 593L,
`595` = 595L, `596` = 596L, `597` = 597L, `598` = 598L, `599` = 599L,
`601` = 601L, `602` = 602L, `603` = 603L, `604` = 604L, `605` = 605L,
`606` = 606L, `608` = 608L, `609` = 609L, `610` = 610L, `611` = 611L,
`612` = 612L, `614` = 614L, `615` = 615L, `616` = 616L, `617` = 617L,
`618` = 618L, `619` = 619L, `620` = 620L, `621` = 621L, `622` = 622L,
`623` = 623L, `624` = 624L, `625` = 625L, `626` = 626L, `627` = 627L,
`628` = 628L, `629` = 629L, `631` = 631L, `632` = 632L, `633` = 633L,
`634` = 634L, `635` = 635L, `636` = 636L, `637` = 637L, `638` = 638L,
`639` = 639L, `640` = 640L, `641` = 641L, `642` = 642L, `643` = 643L,
`645` = 645L, `646` = 646L, `647` = 647L, `648` = 648L, `649` = 649L,
`650` = 650L, `651` = 651L, `652` = 652L, `653` = 653L, `654` = 654L,
`655` = 655L, `657` = 657L, `658` = 658L, `659` = 659L, `661` = 661L,
`662` = 662L, `663` = 663L, `664` = 664L, `666` = 666L, `667` = 667L,
`668` = 668L, `669` = 669L, `670` = 670L, `671` = 671L, `672` = 672L,
`673` = 673L, `675` = 675L, `677` = 677L, `678` = 678L, `679` = 679L,
`680` = 680L, `681` = 681L, `682` = 682L, `683` = 683L, `684` = 684L,
`685` = 685L, `686` = 686L, `687` = 687L, `688` = 688L, `689` = 689L,
`690` = 690L, `691` = 691L, `692` = 692L, `693` = 693L, `696` = 696L,
`697` = 697L, `698` = 698L), class = "omit"))
Your guide="none" implies you don't want a legend for shape. That's why the shapes don't appear. To combine two legends, give them the same name. Thus:
library(ggplot2) # Note typo correction
library(viridis)
ggplot(df, aes(`Lengte_(cm)`, verschil_lengte))+
geom_point(aes(shape = Lengteklasse, colour = Lengteklasse), size = 3)+
geom_hline(yintercept = 1.0, linetype="dashed", color = "red")+
geom_hline(yintercept = 2.0, linetype="dashed", color = "red")+
scale_shape_manual(values = c(16, 17, 15, 3, 8), name="Lengteklasse (cm)")+
scale_color_viridis(discrete = T, option = "D", name="Lengteklasse (cm)")+
scale_x_continuous(breaks = seq(7,12, by = 0.5))+
scale_y_continuous(breaks = seq(0,3, by = 0.5))+
labs(x = "Lengte (cm)", y = "Verschaling (mm)")+
guides(col = guide_legend("Lengteklasse (cm)"))+
theme_classic()
produces
[You can also do away with guides(col = guide_legend("Lengteklasse (cm)"))+.]
Here is another approach. That of #Limey was my first thought. But already posted. Anyway. The clue is:
If you want to have same color and shape in one legend then you have to give them the same name in the aesthetics!
then to give them the same name in one column legend, we have to identify the names argument in scale_color_viridis and scale_shape_manual
then you could remove guides as already stated by Limey!:
library(ggplot)
library(viridis)
ggplot(df, aes(`Lengte_(cm)`, verschil_lengte, shape = Lengteklasse, colour = Lengteklasse))+
geom_point(size = 3)+
geom_hline(yintercept = 1.0, linetype="dashed", color = "red")+
geom_hline(yintercept = 2.0, linetype="dashed", color = "red")+
scale_color_viridis(name = "Lengteklasse (cm)",
discrete = T, option = "D")+
scale_shape_manual(name = "Lengteklasse (cm)",
values = c(16, 17, 15, 3, 8))+
scale_x_continuous(breaks = seq(7,12, by = 0.5))+
scale_y_continuous(breaks = seq(0,3, by = 0.5))+
labs(x = "Lengte (cm)", y = "Verschaling (mm)")+
theme_classic()

How to only select rows that are duplicated in a column in a dataframe

I have joined two dataframes together and I am trying to select only the 'Branch Codes' that are duplicated.
I want to join the datasets 'BranchData' and 'BranchCode' so that any branch codes that are common to both datasets are included as well as those are not common to both datasets.
However, the last line of the code below does not seem to work!
BranchData$'Branch Code' <
as.numeric(BranchData$'Branch Code')
BranchCalls$'Branch Code' <- as.numeric(BranchCalls$'Branch Code')
BranchData <- na.omit(BranchData)
merged <- full_join(BranchData,BranchCalls)
merged <- merged %>% group_by(merged$`Branch Code`) %>% filter(n() >= 2)
Also, when I try to put the duplicates into groups, so that all the duplicates are together, but the following code doesn't seem to work!:
merged <- group_by(merged,merged$'Branch Code')
Minimal Reproducible Example:
structure(list(`Branch Code` = c(401801, 436801, 403801, 164801,
198801), `Location Type` = c("Urban", "Urban", "Urban Deprived",
"Rural", "Urban"), Type = c("MAIN", "MAIN", "MAIN", "MAIN", "LM"
), Status = c("Open", "Open", "Open", "Open", "Open"), Segment = c("Agency",
"Agency", "Agency", "Agency", "Agency"), `Multiple (partner that owns multiple branches)` = c("Multiple 11",
"Multiple 11", "Multiple 12", "Multiple 13", "Multiple 13"),
RetailType = c("Books_Stationery", "Books_Stationery", "Convenience",
"Convenience", "Convenience"), `Volume of transactions` = c(2238,
1514, 1346, 1338, 625), `Open hours` = c(47.75, 50.2500000000001,
46.5, 48.25, 114.25), `X Pos` = c(394169, 393488, 394434,
392153, 393094), `Y Pos` = c(806326, 805877, 804347, 796902,
802789), Urbanity = c("Major Centre", "Major Centre", "High Density",
"Low Density", "Low Density"), `Case Reference Number` = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
), `Created On` = structure(c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), tzone = "UTC", class = c("POSIXct",
"POSIXt")), `Branch Type` = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), L1 = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
), L2 = c(NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_), L3 = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), L4 = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), `Case Type` = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
), na.action = structure(c(`3` = 3L, `4` = 4L, `5` = 5L, `6` = 6L,
`7` = 7L, `8` = 8L, `9` = 9L, `11` = 11L, `13` = 13L, `16` = 16L,
`17` = 17L, `18` = 18L, `20` = 20L, `21` = 21L, `22` = 22L, `23` = 23L,
`26` = 26L, `27` = 27L, `28` = 28L, `29` = 29L, `31` = 31L, `32` = 32L,
`33` = 33L, `34` = 34L, `35` = 35L, `36` = 36L, `37` = 37L, `39` = 39L,
`40` = 40L, `41` = 41L, `42` = 42L, `43` = 43L, `44` = 44L, `45` = 45L,
`46` = 46L, `47` = 47L, `48` = 48L, `49` = 49L, `51` = 51L, `52` = 52L,
`54` = 54L, `55` = 55L, `57` = 57L, `58` = 58L, `59` = 59L, `60` = 60L,
`61` = 61L, `62` = 62L, `63` = 63L, `65` = 65L, `67` = 67L, `68` = 68L,
`69` = 69L, `70` = 70L, `71` = 71L, `72` = 72L, `74` = 74L, `75` = 75L,
`76` = 76L, `77` = 77L, `78` = 78L, `80` = 80L, `81` = 81L, `82` = 82L,
`83` = 83L, `84` = 84L, `86` = 86L, `87` = 87L, `88` = 88L, `89` = 89L,
`91` = 91L, `92` = 92L, `93` = 93L, `96` = 96L, `97` = 97L, `98` = 98L,
`99` = 99L, `100` = 100L, `101` = 101L, `103` = 103L, `106` = 106L,
`107` = 107L, `108` = 108L, `109` = 109L, `110` = 110L, `111` = 111L,
`112` = 112L, `113` = 113L, `114` = 114L, `115` = 115L, `116` = 116L,
`117` = 117L, `118` = 118L, `119` = 119L, `120` = 120L, `121` = 121L,
`122` = 122L, `123` = 123L, `124` = 124L, `126` = 126L, `127` = 127L,
`129` = 129L, `130` = 130L, `131` = 131L, `132` = 132L, `133` = 133L,
`134` = 134L, `135` = 135L, `136` = 136L, `137` = 137L, `139` = 139L,
`140` = 140L, `141` = 141L, `142` = 142L, `143` = 143L, `144` = 144L,
`145` = 145L, `146` = 146L, `147` = 147L, `148` = 148L, `149` = 149L,
`150` = 150L, `151` = 151L, `152` = 152L, `153` = 153L, `155` = 155L,
`156` = 156L, `157` = 157L, `160` = 160L, `161` = 161L, `162` = 162L,
`163` = 163L, `165` = 165L, `166` = 166L, `167` = 167L, `168` = 168L,
`169` = 169L, `174` = 174L, `175` = 175L, `176` = 176L, `177` = 177L,
`178` = 178L, `179` = 179L, `180` = 180L, `182` = 182L, `183` = 183L,
`185` = 185L, `186` = 186L, `188` = 188L, `189` = 189L, `190` = 190L,
`191` = 191L, `192` = 192L, `193` = 193L, `194` = 194L, `195` = 195L,
`196` = 196L, `197` = 197L, `198` = 198L, `199` = 199L, `200` = 200L,
`201` = 201L, `203` = 203L, `204` = 204L, `205` = 205L, `206` = 206L,
`207` = 207L, `209` = 209L, `210` = 210L, `211` = 211L, `212` = 212L,
`213` = 213L, `214` = 214L, `215` = 215L, `216` = 216L, `217` = 217L,
`218` = 218L, `219` = 219L, `220` = 220L, `221` = 221L, `222` = 222L,
`223` = 223L, `224` = 224L, `226` = 226L, `227` = 227L, `228` = 228L,
`229` = 229L, `230` = 230L, `231` = 231L, `232` = 232L, `233` = 233L,
`234` = 234L, `236` = 236L, `237` = 237L, `238` = 238L, `239` = 239L,
`240` = 240L, `241` = 241L, `242` = 242L, `243` = 243L, `244` = 244L,
`245` = 245L, `247` = 247L, `248` = 248L, `249` = 249L, `250` = 250L,
`251` = 251L, `252` = 252L, `253` = 253L, `254` = 254L, `255` = 255L,
`256` = 256L, `257` = 257L, `258` = 258L, `259` = 259L, `260` = 260L,
`261` = 261L, `262` = 262L, `263` = 263L, `264` = 264L, `265` = 265L,
`266` = 266L, `267` = 267L, `268` = 268L, `269` = 269L, `270` = 270L,
`271` = 271L, `272` = 272L, `273` = 273L, `274` = 274L, `276` = 276L,
`278` = 278L, `280` = 280L, `281` = 281L, `282` = 282L, `283` = 283L,
`284` = 284L, `285` = 285L, `286` = 286L, `288` = 288L, `289` = 289L,
`291` = 291L, `292` = 292L, `293` = 293L, `294` = 294L, `296` = 296L,
`297` = 297L, `298` = 298L, `299` = 299L, `300` = 300L, `301` = 301L,
`304` = 304L, `305` = 305L, `306` = 306L, `307` = 307L, `308` = 308L,
`311` = 311L, `312` = 312L, `313` = 313L, `316` = 316L, `319` = 319L,
`321` = 321L, `322` = 322L, `323` = 323L, `324` = 324L, `325` = 325L,
`326` = 326L, `327` = 327L, `328` = 328L, `329` = 329L, `330` = 330L,
`331` = 331L, `332` = 332L, `333` = 333L, `335` = 335L, `337` = 337L,
`338` = 338L, `339` = 339L, `340` = 340L, `341` = 341L, `342` = 342L,
`343` = 343L, `344` = 344L, `345` = 345L, `346` = 346L, `347` = 347L,
`348` = 348L, `349` = 349L, `350` = 350L, `351` = 351L, `352` = 352L,
`353` = 353L, `354` = 354L, `355` = 355L, `356` = 356L, `357` = 357L,
`359` = 359L, `360` = 360L, `361` = 361L, `362` = 362L, `363` = 363L,
`365` = 365L, `366` = 366L, `367` = 367L, `368` = 368L, `370` = 370L,
`371` = 371L, `372` = 372L, `373` = 373L, `375` = 375L, `376` = 376L,
`378` = 378L, `379` = 379L, `380` = 380L, `381` = 381L, `382` = 382L,
`384` = 384L, `385` = 385L, `387` = 387L, `388` = 388L, `389` = 389L,
`390` = 390L, `391` = 391L, `392` = 392L, `393` = 393L, `395` = 395L,
`396` = 396L, `397` = 397L, `398` = 398L, `399` = 399L, `400` = 400L,
`401` = 401L, `403` = 403L, `404` = 404L, `405` = 405L, `409` = 409L,
`412` = 412L, `413` = 413L, `414` = 414L, `415` = 415L, `416` = 416L,
`418` = 418L, `419` = 419L, `420` = 420L, `421` = 421L, `422` = 422L,
`423` = 423L, `426` = 426L, `427` = 427L, `428` = 428L, `429` = 429L,
`432` = 432L, `433` = 433L, `435` = 435L, `436` = 436L, `437` = 437L,
`438` = 438L, `440` = 440L, `441` = 441L, `442` = 442L, `443` = 443L,
I would be so grateful if anybody could give me a helping hand!
Thank you so much!
You can do it using table:
merged %>% filter(table(`Branch Code`)[`Branch Code`] > 1)
or using add_count:
merged %>% add_count(`Branch Code`) %>% filter(n > 1)
I created a small sample data:
merged <- data.frame(branch_code = c("401801", "436801", "401801"),
location_type = c("Urban", "Urban", "Rural"))
branch_code location_type
1 401801 Urban
2 436801 Urban
3 401801 Rural
You can use this code:
merged %>%
group_by(branch_code) %>%
mutate(n = n()) %>%
filter(n > 1) %>%
select(-n)
Output:
# A tibble: 2 × 2
# Groups: branch_code [1]
branch_code location_type
<chr> <chr>
1 401801 Urban
2 401801 Rural

R nls() Initial Parameter Problem, nonlinear Regression

I get a error message:
Error in nlsModel(formula, mf, start, wts) :
singular gradient matrix at initial parameter estimates
when using the nls() function like
form_Q10_parabolic_SM <- as.formula(Lin_Flux..mymol.m.2.s.1. ~ (rRef<- 5.5354)*a*exp(b*Mean_Soil_Temp_V2..C.)*((-c*Soil_Moist_V3**2)+(d*Soil_Moist_V3)+e))
Q10_parabolic_SM <- nls(form_Q10_parabolic_SM, data = conB1_2015, start = list(a = 1, b = 0.11, c = 0.0001, d = 0.01, e = 0.1))
I got my initial parameters by using the preview() function of the nsltools library like this (same definition of the formula like above)
preview(form_Q10_parabolic_SM, data = conB1_2015, start = c(a = 1, b = 0.11, c = 0.0001, d = 0.01, e = 0.1), variable = 1)
Which gives me this output with the parameters a-e above:
This looks quite good by my eyes and I really don't know what to do at this point since the preview() works just fine.
Is my model too complex or overparameterized? Or did I just do something wrong with the nls function?
Any tips would be really appreciated!
> dput(head(conB1_2015, 30))
structure(list(X = c(13L, 68L, 69L, 70L, 71L, 72L, 73L, 74L,
75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L,
88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L), IV_Date = c("2015-01-14",
"2015-03-11", "2015-03-12", "2015-03-13", "2015-03-14", "2015-03-15",
"2015-03-16", "2015-03-17", "2015-03-18", "2015-03-19", "2015-03-20",
"2015-03-21", "2015-03-22", "2015-03-23", "2015-03-24", "2015-03-25",
"2015-03-26", "2015-03-27", "2015-03-28", "2015-03-29", "2015-03-30",
"2015-03-31", "2015-04-01", "2015-04-02", "2015-04-03", "2015-04-04",
"2015-04-05", "2015-04-06", "2015-04-07", "2015-04-08"), SMmean010.... = c(24.5341666666667,
23.4754166666667, 23.0585416666667, 22.830625, 22.7447916666667,
22.7729166666666, 22.7929166666667, 22.7354166666667, 22.6579166666667,
22.5935416666667, 22.5233333333333, 22.7641666666667, 23.6010416666667,
23.445625, 23.404375, 23.2845833333333, 23.0672916666667, 22.9347916666667,
22.8272916666667, 23.0316666666667, 23.988125, 25.5647916666667,
27.055, 27.7995833333333, 26.23125, 25.4658333333333, 25.0845833333333,
24.8175, 24.605, 24.4216666666667), Lin_Flux..mymol.m.2.s.1. = c(1.13,
2.146, 1.98708333333333, 1.88416666666667, 1.57083333333333,
1.93041666666667, 2.69875, 2.8075, 3.23272727272727, 2.35818181818182,
2.23833333333333, 1.84958333333333, 2.18695652173913, 2.16958333333333,
2.69791666666667, 3.025, 1.985, 1.88083333333333, 2.30416666666667,
2.775, 1.44458333333333, 1.78791666666667, 1.04863636363636,
1.03458333333333, 1.4725, 1.86833333333333, 1.71125, 1.79, 1.53166666666667,
1.97666666666667), Mean_Soil_Temp_V2..C. = c(4.739, 5.1864, 4.08408333333333,
3.61625, 3.68508333333333, 4.09925, 4.87079166666667, 5.64720833333333,
6.58433333333333, 5.05075, 4.93708333333333, 4.109, 3.2295, 3.537,
5.1395, 5.65270833333333, 5.931875, 5.61775, 5.88695833333333,
6.86308333333333, 5.61833333333333, 4.24566666666667, 3.05952173913043,
2.45716666666667, 3.6365, 3.68820833333333, 3.83766666666667,
4.3435, 4.8745, 6.29133333333333), Soil_Moist_V3 = c(25.603137,
21.98744709, 21.8053864833333, 21.6770563291667, 20.1319423708333,
19.9826592666667, 19.8279438958333, 20.1589541791667, 21.5796382,
21.5971315083333, 21.3742824541667, 21.8992939333333, 23.9737254583333,
23.4506886041667, 23.0956395708333, 22.574581225, 22.3561680833333,
21.3806269916667, 21.4045219791667, 21.5611478916667, 25.5090813166667,
28.6440265, 31.4434210347826, 31.9276734541667, 27.5706909333333,
25.1139413583333, 24.2945348333333, 24.0232171416667, 23.705631425,
22.8323341625), precip50..mm. = c(0.6, 0, 0, 0, 0.9, 1.3, 0,
0, 0, 0, 0, 6.6, 0, 0, 0, 0, 0.1, 0.2, 0.1, 6.1, 5, 17.6, 10.4,
6.6, 0, 0, 0, 0, 0, 0), RWI = c(0.6, 0.4, 0.2, 0.133333333333333,
0.9, 1.3, 1.3, 0.65, 0.433333333333333, 0.325, 0.26, 6.6, 6.6,
3.3, 2.2, 1.65, 0.1, 0.2, 0.1, 6.1, 5, 17.6, 10.4, 6.6, 6.6,
3.3, 2.2, 1.65, 1.32, 1.1)), na.action = structure(c(`1` = 1L,
`2` = 2L, `3` = 3L, `4` = 4L, `5` = 5L, `6` = 6L, `7` = 7L, `8` = 8L,
`9` = 9L, `10` = 10L, `11` = 11L, `12` = 12L, `13` = 13L, `15` = 15L,
`16` = 16L, `17` = 17L, `18` = 18L, `19` = 19L, `20` = 20L, `21` = 21L,
`22` = 22L, `23` = 23L, `24` = 24L, `25` = 25L, `26` = 26L, `27` = 27L,
`28` = 28L, `29` = 29L, `30` = 30L, `31` = 31L, `32` = 32L, `33` = 33L,
`34` = 34L, `35` = 35L, `36` = 36L, `37` = 37L, `38` = 38L, `39` = 39L,
`40` = 40L, `41` = 41L, `42` = 42L, `43` = 43L, `44` = 44L, `45` = 45L,
`46` = 46L, `47` = 47L, `48` = 48L, `49` = 49L, `50` = 50L, `51` = 51L,
`52` = 52L, `53` = 53L, `54` = 54L, `55` = 55L, `56` = 56L, `57` = 57L,
`58` = 58L, `59` = 59L, `60` = 60L, `61` = 61L, `62` = 62L, `63` = 63L,
`64` = 64L, `65` = 65L, `66` = 66L, `67` = 67L, `68` = 68L, `199` = 199L,
`218` = 218L, `219` = 219L, `220` = 220L, `221` = 221L, `222` = 222L,
`223` = 223L, `224` = 224L, `225` = 225L, `226` = 226L, `227` = 227L,
`228` = 228L, `229` = 229L, `230` = 230L, `231` = 231L, `232` = 232L,
`264` = 264L, `265` = 265L, `266` = 266L, `267` = 267L, `352` = 352L,
`353` = 353L, `354` = 354L, `355` = 355L, `356` = 356L, `357` = 357L,
`358` = 358L, `359` = 359L, `360` = 360L, `361` = 361L, `362` = 362L,
`363` = 363L, `364` = 364L, `365` = 365L, `366` = 366L), class = "omit"), row.names = c(14L,
69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L,
82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L,
95L, 96L, 97L), class = "data.frame")
The main problem is that the parameters are not uniquely identifiable. We can multiply a by an arbitrary number and divide c, d and e by that same number and we get the same model. Omit a.
Although it won't hurt the use of as.formula is redundant since it is already a formula.
Having an assignment within an nls formula is highly unusual. nls will think that Rref is a parameter and fail on that account. Remove the assignment.
If we make these changes then it does give an answer with the data in the updated version of the question.
form_Q10_parabolic_SM <- Lin_Flux..mymol.m.2.s.1. ~
exp(b*Mean_Soil_Temp_V2..C.) * ( (-c*Soil_Moist_V3**2) + (d*Soil_Moist_V3) + e)
Q10_parabolic_SM <- nls(form_Q10_parabolic_SM, data = conB1_2015,
start = list(b = 0.11, c = 0.0001, d = 0.01, e = 0.1))
giving:
> Q10_parabolic_SM
Nonlinear regression model
model: Lin_Flux..mymol.m.2.s.1. ~ exp(b * Mean_Soil_Temp_V2..C.) * ((-c * Soil_Moist_V3^2) + (d * Soil_Moist_V3) + e)
data: conB1_2015
b c d e
0.103062 -0.001564 -0.135531 3.528621
residual sum-of-squares: 3.979
Number of iterations to convergence: 6
Achieved convergence tolerance: 4.401e-06
plinear
Note that nls also has the plinear algorithm which has the advantage that only nonlinear parameters (in this case only b) need starting values. In that case the formula's RHS should be a matrix with the columns that multiply each linear parameter. It gives the same answer as above except the linear parameters are given names starting with .lin . Note that the plinear version converges in fewer iterations than the version using the default algorithm above. (Also it seems that the plinear version is not very sensitive to the starting value and even if we use b=1 as the starting value it converges.)
fo <- Lin_Flux..mymol.m.2.s.1. ~
cbind(-Soil_Moist_V3**2, Soil_Moist_V3, 1) * exp(b*Mean_Soil_Temp_V2..C.)
fm <- nls(fo, data = conB1_2015, start = list(b = 0.11), algorithm = "plinear")
giving:
> fm
Nonlinear regression model
model: Lin_Flux..mymol.m.2.s.1. ~ cbind(-Soil_Moist_V3^2, Soil_Moist_V3, 1) * exp(b * Mean_Soil_Temp_V2..C.)
data: conB1_2015
b .lin1 .lin.Soil_Moist_V3 .lin3
0.103062 -0.001564 -0.135528 3.528593
residual sum-of-squares: 3.979
Number of iterations to convergence: 3
Achieved convergence tolerance: 2.189e-06

Error:'x' must be an array of at least two dimensions when using rowMeans() in a large dataframe

This is the dataframe I'm working with:
> dput(act.byDay)
list(`6/20/2018` = c(`0` = 59L, `1` = 74L, `2` = 2683L, `3` = 4341L,
`4` = 3676L, `5` = 2143L, `6` = 3890L, `7` = 3887L, `8` = 1299L,
`9` = 1492L, `10` = 3449L, `11` = 2200L, `12` = 1563L, `13` = 4346L,
`14` = 5329L, `15` = 3037L, `16` = 1462L, `17` = 668L, `18` = 383L,
`19` = 483L, `20` = 288L, `21` = 2765L, `22` = 3354L, `23` = 1783L
), `6/21/2018` = c(`0` = 241L, `1` = 301L, `2` = 261L, `3` = 3683L,
`4` = 4356L, `5` = 3736L, `6` = 2810L, `7` = 1841L, `8` = 3146L,
`9` = 609L, `10` = 2998L, `11` = 4059L, `12` = 3690L, `13` = 3735L,
`14` = 1343L, `15` = 2087L, `16` = 894L, `17` = 341L, `18` = 240L,
`19` = 2113L, `20` = 1684L, `21` = 3115L, `22` = 2890L, `23` = 138L
), `6/22/2018` = c(`0` = 21L, `1` = 451L, `2` = 96L, `3` = 2918L,
`4` = 2279L, `5` = 2282L, `6` = 4992L, `7` = 698L, `8` = 427L,
`9` = 581L, `10` = 1248L, `11` = 2184L, `12` = 1980L, `13` = 2364L,
`14` = 568L, `15` = 2477L, `16` = 525L, `17` = 433L, `18` = 974L,
`19` = 501L, `20` = 760L, `21` = 67L, `22` = 297L, `23` = 1198L
), `6/23/2018` = c(`0` = 2L, `1` = 39L, `2` = 42L, `3` = 1182L,
`4` = 1749L, `5` = 2144L, `6` = 3123L, `7` = 1170L, `8` = 1641L,
`9` = 1112L, `10` = 1526L, `11` = 1199L, `12` = 534L, `13` = 1481L,
`14` = 2388L, `15` = 2756L, `16` = 392L, `17` = 112L, `18` = 390L,
`19` = 107L, `20` = 709L, `21` = 1122L, `22` = 1562L, `23` = 451L
), `6/24/2018` = c(`0` = 8L, `1` = 74L, `2` = 0L, `3` = 158L,
`4` = 780L, `5` = 3118L, `6` = 3292L, `7` = 2759L, `8` = 3121L,
`9` = 2051L, `10` = 2387L, `11` = 900L, `12` = 627L, `13` = 904L,
`14` = 4283L, `15` = 3726L, `16` = 1273L, `17` = 977L, `18` = 326L,
`19` = 163L, `20` = 1915L, `21` = 1073L, `22` = 1021L, `23` = 545L
), `6/25/2018` = c(`0` = 36L, `1` = 22L, `2` = 3L, `3` = 55L,
`4` = 124L, `5` = 22L, `6` = 4093L, `7` = 2867L, `8` = 3649L,
`9` = 2550L, `10` = 1590L, `11` = 636L, `12` = 2571L, `13` = 998L,
`14` = 1066L, `15` = 2967L, `16` = 1211L, `17` = 51L, `18` = 1188L,
`19` = 1413L, `20` = 714L, `21` = 177L, `22` = 132L, `23` = 29L
), `6/26/2018` = c(`0` = 22L, `1` = 43L, `2` = 0L, `3` = 90L,
`4` = 1094L, `5` = 1655L, `6` = 2643L, `7` = 2108L, `8` = 2249L,
`9` = 2453L, `10` = 2857L, `11` = 915L, `12` = 437L, `13` = 1142L,
`14` = 2193L, `15` = 2993L, `16` = 1139L, `17` = 1549L, `18` = 652L,
`19` = 580L, `20` = 970L, `21` = 674L, `22` = 211L, `23` = 206L
), `6/27/2018` = c(`0` = 167L, `1` = 63L, `2` = 1L, `3` = 786L,
`4` = 617L, `5` = 1575L, `6` = 2237L, `7` = 1302L, `8` = 1149L,
`9` = 2009L, `10` = 2234L, `11` = 1263L, `12` = 1259L, `13` = 2017L,
`14` = 1641L, `15` = 2683L, `16` = 1184L, `17` = 449L, `18` = 65L,
`19` = 956L, `20` = 1538L, `21` = 1287L, `22` = 593L, `23` = 362L
), `6/28/2018` = c(`0` = 594L, `1` = 1172L, `2` = 25L, `3` = 445L,
`4` = 921L, `5` = 1812L, `6` = 2235L, `7` = 1153L, `8` = 422L,
`9` = 1084L, `10` = 2158L, `11` = 1610L, `12` = 845L, `13` = 1187L,
`14` = 2528L, `15` = 2161L, `16` = 976L, `17` = 19L, `18` = 747L,
`19` = 570L, `20` = 576L, `21` = 19L, `22` = 304L, `23` = 2L),
`6/29/2018` = c(`0` = 301L, `1` = 7L, `2` = 399L, `3` = 494L,
`4` = 723L, `5` = 1088L, `6` = 771L, `7` = 85L, `8` = 1338L,
`9` = 866L, `10` = 384L, `11` = 1356L, `12` = 2862L, `13` = 3805L,
`14` = 2142L, `15` = 1655L, `16` = 249L, `17` = 235L, `18` = 3L,
`19` = 0L, `20` = 283L, `21` = 981L, `22` = 634L, `23` = 1370L
), `6/30/2018` = c(`0` = 9L, `1` = 137L, `2` = 33L, `3` = 975L,
`4` = 1690L, `5` = 1639L, `6` = 985L, `7` = 210L, `8` = 1266L,
`9` = 2135L, `10` = 2080L, `11` = 1704L, `12` = 2449L, `13` = 3133L,
`14` = 1055L, `15` = 3222L, `16` = 1152L, `17` = 173L, `18` = 858L,
`19` = 188L, `20` = 700L, `21` = 330L, `22` = 905L, `23` = 1232L
), `7/1/2018` = c(`0` = 1006L, `1` = 5L, `2` = 21L, `3` = 520L,
`4` = 1162L, `5` = 1771L, `6` = 2463L, `7` = 1403L, `8` = 1353L,
`9` = 1938L, `10` = 2388L, `11` = 4133L, `12` = 900L, `13` = 2660L,
`14` = 3504L, `15` = 3946L, `16` = 1956L, `17` = 818L, `18` = 604L,
`19` = 937L, `20` = 373L, `21` = 48L, `22` = 400L, `23` = 201L
), `7/10/2018` = c(`0` = 705L, `1` = 47L, `2` = 605L, `3` = 257L,
`4` = 1359L, `5` = 41L, `6` = 1019L, `7` = 1426L, `8` = 2219L,
`9` = 1179L, `10` = 1624L, `11` = 537L, `12` = 421L, `13` = 1747L,
`14` = 2941L, `15` = 2921L, `16` = 1046L, `17` = 283L, `18` = 476L,
`19` = 218L, `20` = 59L, `21` = 389L, `22` = 657L, `23` = 1293L
), `7/11/2018` = c(`0` = 24L, `1` = 455L, `2` = 6L, `3` = 1232L,
`4` = 2264L, `5` = 1152L, `6` = 600L, `7` = 11L, `8` = 980L,
`9` = 1519L, `10` = 2004L, `11` = 1933L, `12` = 2161L, `13` = 1386L,
`14` = 1883L, `15` = 2978L, `16` = 1385L, `17` = 104L, `18` = 1309L,
`19` = 2L, `20` = 364L, `21` = 550L, `22` = 0L, `23` = 1433L
), `7/12/2018` = c(`0` = 1634L, `1` = 27L, `2` = 860L, `3` = 1095L,
`4` = 1102L, `5` = 132L, `6` = 582L, `7` = 710L, `8` = 1368L,
`9` = 2470L, `10` = 2944L, `11` = 1030L, `12` = 1286L, `13` = 387L,
`14` = 2590L, `15` = 2449L, `16` = 743L, `17` = 134L, `18` = 274L,
`19` = 205L, `20` = 360L, `21` = 627L, `22` = 1357L, `23` = 591L
), `7/13/2018` = c(`0` = 216L, `1` = 143L, `2` = 70L, `3` = 2L,
`4` = 477L, `5` = 42L, `6` = 81L, `7` = 304L, `8` = 2827L,
`9` = 2437L, `10` = 2002L, `11` = 688L, `12` = 935L, `13` = 812L,
`14` = 404L, `15` = 1098L, `16` = 1157L, `17` = 857L, `18` = 466L,
`19` = 215L, `20` = 714L, `21` = 269L, `22` = 1223L, `23` = 8L
), `7/14/2018` = c(`0` = 1L, `1` = 635L, `2` = 6L, `3` = 1797L,
`4` = 1363L, `5` = 246L, `6` = 704L, `7` = 1089L, `8` = 943L,
`9` = 2251L, `10` = 813L, `11` = 2643L, `12` = 1657L, `13` = 18L,
`14` = 1132L, `15` = 2884L, `16` = 1044L, `17` = 149L, `18` = 1146L,
`19` = 68L, `20` = 1227L, `21` = 1189L, `22` = 129L, `23` = 1291L
), `7/15/2018` = c(`0` = 7L, `1` = 9L, `2` = 1299L, `3` = 389L,
`4` = 288L, `5` = 157L, `6` = 0L, `7` = 324L, `8` = 248L,
`9` = 915L, `10` = 795L, `11` = 598L, `12` = 733L, `13` = 308L,
`14` = 2760L, `15` = 2874L, `16` = 1903L, `17` = 499L, `18` = 73L,
`19` = 31L, `20` = 1146L, `21` = 920L, `22` = 852L, `23` = 2L
), `7/16/2018` = c(`0` = 104L, `1` = 564L, `2` = 16L, `3` = 1903L,
`4` = 675L, `5` = 1859L, `6` = 720L, `7` = 1017L, `8` = 4L,
`9` = 2114L, `10` = 2264L, `11` = 1152L, `12` = 935L, `13` = 1691L,
`14` = 1031L, `15` = 2568L, `16` = 2035L, `17` = 226L, `18` = 18L,
`19` = 1716L, `20` = 249L, `21` = 717L, `22` = 635L, `23` = 919L
), `7/17/2018` = c(`0` = 1436L, `1` = 16L, `2` = 17L, `3` = 1891L,
`4` = 1175L, `5` = 74L, `6` = 435L, `7` = 377L, `8` = 718L,
`9` = 619L, `10` = 439L, `11` = 1373L, `12` = 2154L, `13` = 2481L,
`14` = 763L, `15` = 2084L, `16` = 910L, `17` = 641L, `18` = 669L,
`19` = 737L, `20` = 793L, `21` = 1471L, `22` = 12L, `23` = 96L
), `7/18/2018` = c(`0` = 6L, `1` = 13L, `2` = 81L, `3` = 1227L,
`4` = 1685L, `5` = 260L, `6` = 238L, `7` = 575L, `8` = 930L,
`9` = 330L, `10` = 1139L, `11` = 785L, `12` = 1110L, `13` = 1007L,
`14` = 1770L, `15` = 2824L, `16` = 729L, `17` = 776L, `18` = 602L,
`19` = 550L, `20` = 1432L, `21` = 567L, `22` = 197L, `23` = 107L
), `7/19/2018` = c(`0` = 38L, `1` = 648L, `2` = 264L, `3` = 911L,
`4` = 2239L, `5` = 1063L, `6` = 9L, `7` = 1336L, `8` = 1235L,
`9` = 628L, `10` = 1722L, `11` = 1028L, `12` = 1393L, `13` = 44L,
`14` = 2110L, `15` = 1719L, `16` = 666L, `17` = 127L, `18` = 885L,
`19` = 788L, `20` = 1274L, `21` = 765L, `22` = 1094L, `23` = 38L
), `7/2/2018` = c(`0` = 876L, `1` = 505L, `2` = 162L, `3` = 775L,
`4` = 1567L, `5` = 896L, `6` = 1648L, `7` = 995L, `8` = 2574L,
`9` = 1080L, `10` = 997L, `11` = 1881L, `12` = 1375L, `13` = 1283L,
`14` = 2156L, `15` = 2384L, `16` = 982L, `17` = 33L, `18` = 20L,
`19` = 761L, `20` = 241L, `21` = 696L, `22` = 133L, `23` = 915L
), `7/20/2018` = c(`0` = 514L, `1` = 14L, `2` = 59L, `3` = 1081L,
`4` = 1266L, `5` = 359L, `6` = 1055L, `7` = 280L, `8` = 123L,
`9` = 2251L, `10` = 2302L, `11` = 1116L, `12` = 2750L, `13` = 764L,
`14` = 1377L, `15` = 2776L, `16` = 970L, `17` = 814L, `18` = 10L,
`19` = 1364L, `20` = 1137L, `21` = 279L, `22` = 10L, `23` = 605L
), `7/21/2018` = c(`0` = 279L, `1` = 596L, `2` = 12L, `3` = 1443L,
`4` = 1463L, `5` = 1426L, `6` = 132L, `7` = 924L, `8` = 379L,
`9` = 693L, `10` = 137L, `11` = 219L, `12` = 884L, `13` = 194L,
`14` = 450L, `15` = 1204L, `16` = 487L, `17` = 578L, `18` = 445L,
`19` = 9L, `20` = 823L, `21` = 2L, `22` = 1212L, `23` = 12L
), `7/22/2018` = c(`0` = 200L, `1` = 9L, `2` = 152L, `3` = 1062L,
`4` = 1926L, `5` = 1156L, `6` = 1951L, `7` = 1735L, `8` = 753L,
`9` = 570L, `10` = 362L, `11` = 813L, `12` = 756L, `13` = 1403L,
`14` = 308L, `15` = 1895L, `16` = 325L, `17` = 768L, `18` = 666L,
`19` = 33L, `20` = 634L, `21` = 1294L, `22` = 819L, `23` = 39L
), `7/23/2018` = c(`0` = 579L, `1` = 8L, `2` = 657L, `3` = 438L,
`4` = 521L, `5` = 896L, `6` = 2560L, `7` = 1383L, `8` = 819L,
`9` = 1293L, `10` = 2257L, `11` = 476L, `12` = 1850L, `13` = 759L,
`14` = 2482L, `15` = 1513L, `16` = 789L, `17` = 78L, `18` = 329L,
`19` = 43L, `20` = 50L, `21` = 1583L, `22` = 342L, `23` = 0L
), `7/24/2018` = c(`0` = 0L), `7/3/2018` = c(`0` = 623L,
`1` = 918L, `2` = 48L, `3` = 523L, `4` = 721L, `5` = 1624L,
`6` = 1047L, `7` = 1783L, `8` = 313L, `9` = 1042L, `10` = 2211L,
`11` = 2430L, `12` = 1770L, `13` = 1610L, `14` = 2814L, `15` = 2460L,
`16` = 1770L, `17` = 25L, `18` = 709L, `19` = 416L, `20` = 709L,
`21` = 998L, `22` = 921L, `23` = 89L), `7/4/2018` = c(`0` = 258L,
`1` = 1242L, `2` = 75L, `3` = 1131L, `4` = 893L, `5` = 402L,
`6` = 381L, `7` = 51L, `8` = 15L, `9` = 47L, `10` = 762L,
`11` = 777L, `12` = 479L, `13` = 2416L, `14` = 3639L, `15` = 1991L,
`16` = 202L, `17` = 1054L, `18` = 917L, `19` = 1565L, `20` = 503L,
`21` = 61L, `22` = 44L, `23` = 2103L), `7/5/2018` = c(`0` = 2212L,
`1` = 352L, `2` = 1L, `3` = 666L, `4` = 351L, `5` = 1321L,
`6` = 7L, `7` = 1010L, `8` = 1222L, `9` = 1080L, `10` = 1643L,
`11` = 1101L, `12` = 188L, `13` = 2793L, `14` = 1548L, `15` = 1811L,
`16` = 1807L, `17` = 51L, `18` = 788L, `19` = 1108L, `20` = 1157L,
`21` = 1038L, `22` = 225L, `23` = 454L), `7/6/2018` = c(`0` = 441L,
`1` = 376L, `2` = 444L, `3` = 5L, `4` = 501L, `5` = 579L,
`6` = 1253L, `7` = 1600L, `8` = 1051L, `9` = 498L, `10` = 2217L,
`11` = 2362L, `12` = 2425L, `13` = 1220L, `14` = 2037L, `15` = 2684L,
`16` = 799L, `17` = 471L, `18` = 139L, `19` = 545L, `20` = 1117L,
`21` = 177L, `22` = 487L, `23` = 1420L), `7/7/2018` = c(`0` = 692L,
`1` = 303L, `2` = 736L, `3` = 750L, `4` = 1386L, `5` = 926L,
`6` = 30L, `7` = 862L, `8` = 1912L, `9` = 2731L, `10` = 1123L,
`11` = 1160L, `12` = 2892L, `13` = 1634L, `14` = 585L, `15` = 3473L,
`16` = 2243L, `17` = 441L, `18` = 399L, `19` = 1482L, `20` = 111L,
`21` = 455L, `22` = 1315L, `23` = 691L), `7/8/2018` = c(`0` = 1428L,
`1` = 96L, `2` = 52L, `3` = 258L, `4` = 1135L, `5` = 1727L,
`6` = 448L, `7` = 2148L, `8` = 358L, `9` = 2180L, `10` = 1519L,
`11` = 2634L, `12` = 828L, `13` = 1212L, `14` = 1052L, `15` = 2851L,
`16` = 902L, `17` = 171L, `18` = 236L, `19` = 3L, `20` = 727L,
`21` = 1366L, `22` = 637L, `23` = 43L), `7/9/2018` = c(`0` = 0L,
`1` = 1320L, `2` = 146L, `3` = 664L, `4` = 862L, `5` = 663L,
`6` = 227L, `7` = 227L, `8` = 995L, `9` = 743L, `10` = 1793L,
`11` = 2421L, `12` = 1346L, `13` = 1874L, `14` = 2182L, `15` = 1333L,
`16` = 1967L, `17` = 1023L, `18` = 297L, `19` = 340L, `20` = 1469L,
`21` = 10L, `22` = 213L, `23` = 805L))
The object has several columns with a date on the header and several rows labelling the hour of that date. The values in the table represent average speed measures of an animal for every hour for the days listed in the column headers.
I would like to create a dataframe or to add a column that takes the average for every hour for all days. In other words, this means applying the function rowMeans().
However when using it, I'm getting:
> act.byDay$overall <- rowMeans(act.byDay)
Error in rowMeans(act.byDay) :
'x' must be an array of at least two dimensions
If somebody knows what I'm doing wrong, I would appreciate your help!
The 'act.byDay' is a list of named vectors by checking the structure (str(act.byDay)). We can rbind the vectors together with do.call and then apply rowMeans
rowMeans(do.call(rbind, act.byDay))
Or as #r2evans mentioned, it could be also
rowMeans(as.data.frame(act.byDay))

Resources