How to combine 3 data in a table?

Hi,

I came here a month ago because I have been assigned at school a task with R Studio and I am trying to do it
( how to combine three different data from a dataset? - General - RStudio Community)

I am struggling with what I need to do :frowning: I need to combine data from three tabs but do not know which command to use....

There are some variables, namely ccaa (which means destination), pais (which means country of origine) and gmedio (which means average expense). I need to explore two variables from ccaa in one pais so that I can compare gmedio in those two ccaa... sorry if I dont explain myself correctly, but i have very basic knowledge...

anyone can help?

1 — ImgBB for the screenshot

thank you so much

Please provide some minimally useable code (not a screenshot) -- it is very difficult to help otherwise.

A reprex provided in the previous question contains the data

d <- data.frame(pais = c(
  10L, 15L, 15L, 10L, 15L, 15L, 15L, 15L,
  10L, 15L, 15L, 15L, 15L, 15L, 10L, 10L, 10L, 6L, 6L, 6L, 6L,
  6L, 6L, 6L, 15L
), ccaa = c(
  16L, 13L, 13L, 13L, 13L, 7L, 9L, 12L,
  17L, 9L, 13L, 9L, 9L, 7L, 9L, 9L, 9L, 4L, 4L, 4L, 4L, 4L, 4L,
  4L, 9L
), gastototal = c(
  2104.8327842, 3339.4242179, 2527.0036036,
  1076.4172965, 2812.0859804, 6686.339234, 2196.4232084, 6941.6912321,
  2085.3796211, 3355.3558589, 3422.6737527, 2697.8463611, 1750.2838905,
  4599.6563689, 1444.3916741, 1403.0404296, 1345.220257, 1303.2814619,
  704.5863107, 1345.4311241, 1495.1874334, 1074.8804203, 1345.4311241,
  1252.3900093, 997.72492846))

It appears that the objective is to subset the data by the pais variable, and from the screenshot the failure is arising from using the name table for the object. Because table is the name of a built-in function it cannot be subset and throws an error

object of type 'closure' is not subsettable

Using d, however, allows the subset to proceed by asking, in effect

  1. How many unique values of pais occur in the data frame?
  2. In which rows of the data frame do they occur?
d <- data.frame(pais = c(
  10L, 15L, 15L, 10L, 15L, 15L, 15L, 15L,
  10L, 15L, 15L, 15L, 15L, 15L, 10L, 10L, 10L, 6L, 6L, 6L, 6L,
  6L, 6L, 6L, 15L
), ccaa = c(
  16L, 13L, 13L, 13L, 13L, 7L, 9L, 12L,
  17L, 9L, 13L, 9L, 9L, 7L, 9L, 9L, 9L, 4L, 4L, 4L, 4L, 4L, 4L,
  4L, 9L
), gastototal = c(
  2104.8327842, 3339.4242179, 2527.0036036,
  1076.4172965, 2812.0859804, 6686.339234, 2196.4232084, 6941.6912321,
  2085.3796211, 3355.3558589, 3422.6737527, 2697.8463611, 1750.2838905,
  4599.6563689, 1444.3916741, 1403.0404296, 1345.220257, 1303.2814619,
  704.5863107, 1345.4311241, 1495.1874334, 1074.8804203, 1345.4311241,
  1252.3900093, 997.72492846))


unique(d$pais)
#> [1] 10 15  6

p6 <- which(d$pais == 6)
p10 <- which(d$pais == 10)
p15 <- which(d$pais == 15)

d[p6,]
#>    pais ccaa gastototal
#> 18    6    4  1303.2815
#> 19    6    4   704.5863
#> 20    6    4  1345.4311
#> 21    6    4  1495.1874
#> 22    6    4  1074.8804
#> 23    6    4  1345.4311
#> 24    6    4  1252.3900
d[p10,]
#>    pais ccaa gastototal
#> 1    10   16   2104.833
#> 4    10   13   1076.417
#> 9    10   17   2085.380
#> 15   10    9   1444.392
#> 16   10    9   1403.040
#> 17   10    9   1345.220
d[p15,]
#>    pais ccaa gastototal
#> 2    15   13  3339.4242
#> 3    15   13  2527.0036
#> 5    15   13  2812.0860
#> 6    15    7  6686.3392
#> 7    15    9  2196.4232
#> 8    15   12  6941.6912
#> 10   15    9  3355.3559
#> 11   15   13  3422.6738
#> 12   15    9  2697.8464
#> 13   15    9  1750.2839
#> 14   15    7  4599.6564
#> 25   15    9   997.7249
1 Like

Thank you so much for your help but I still don't get it ;//// I use your coding and it keeps giving me errors ;//// I'll keep trying anyway,
Thanks a lot!

Please post the errors here.

I guess my problem is further away... I just do not know how to get to where you are :confused: it's a bit frustrating :confused: I guess I'd need a teacher by my side :frowning:

I mean I have the original dataset, I have learnt how to add a new column... and I just don't know how to follow. I write the code you provide me with and it does not work because I guess you are using the reprex code I sent but the actual dataset is bigger and I am lost in the middle of all coding. For example, if I write

> unique(d$pais)

I get

Error in unique(d$pais) : object 'd' not found

Thanks a lot anyway to all of you. I think this is too much for me :confused:

I am not sure I understand exactly what you need but if I use your sample dato

dat1 <-  data.frame(pais = c(
  10L, 15L, 15L, 10L, 15L, 15L, 15L, 15L,
  10L, 15L, 15L, 15L, 15L, 15L, 10L, 10L, 10L, 6L, 6L, 6L, 6L,
  6L, 6L, 6L, 15L
), ccaa = c(
  16L, 13L, 13L, 13L, 13L, 7L, 9L, 12L,
  17L, 9L, 13L, 9L, 9L, 7L, 9L, 9L, 9L, 4L, 4L, 4L, 4L, 4L, 4L,
  4L, 9L
), gastototal = c(
  2104.8327842, 3339.4242179, 2527.0036036,
  1076.4172965, 2812.0859804, 6686.339234, 2196.4232084, 6941.6912321,
  2085.3796211, 3355.3558589, 3422.6737527, 2697.8463611, 1750.2838905,
  4599.6563689, 1444.3916741, 1403.0404296, 1345.220257, 1303.2814619,
  704.5863107, 1345.4311241, 1495.1874334, 1074.8804203, 1345.4311241,
  1252.3900093, 997.72492846))

I can get a breakdown of gastototal bp pais and ccaa like this:

library(tidyverse)
dat1 %>% group_by(pais, ccaa) %>% 
    summarise(total = sum(gastototal))

Is this of any help?

In my example, d is the name of the data frame created by the dput of your example.

First, get my example to run by cutting and pasting it.

Second, use a name for your data, but not: table, dt, data. Instead p, use table1.

Third, edit my example to replace d with tab1

Sorry for wasting your time, but I just don't understand anything, I'll leave it here. Thank you so much anyway, I'll pass on this task :frowning: