Re: [R] How to Reformat a dataframe

2023-10-28 Thread jim holtman
You can also use the pivot_longer to do it:

library(tidyverse)

input <- structure(list(...1 = c(92.9925354, 76.0024254, 44.99547465,
28.00536465, 120.0068103, 31.9980405, 85.0071837, 40.1532933,
19.3120917, 113.12581575, 28.45843425, 114.400074, 143.925,
46.439634, 20.7845679, 50.82874575, 36.9818061, 44.6273556, 40.57804605,
:
:
:
 row.names = c(NA, -126L), class = "data.frame")

> input$row <- seq(nrow(input))  # add row number for reference> head(input)
>...1 ...2 ...3 ...4 ...5 ...6 ...7 ...8
1  92.99254 34.99963 24.04101 43.01330 53.00914 62.01390 91.01036 88.99986
2  76.00243 22.00219 22.00219 25.00378 44.99547 60.99449 63.00499 92.99254
3  44.99547 22.99328 15.00793 15.99902 38.00121 44.00438 62.01390 79.99510
4  28.00536 19.00061 12.99743 12.99743 44.00438 49.01647 55.95410 69.00816
5 120.00681 35.99072 22.99328 47.99706 60.00341 62.01390 60.00341 66.00658
6  31.99804 23.95606 13.98852 15.99902 38.99230 54.99132 88.00877 89.99095
  ...9...10 ...11 ...12 row
1 54.00023 75.01134 111.99314  49.01647   1
2 68.01707 75.01134  82.99669  63.99608   2
3 60.99449 91.01036  84.01609  65.01549   3
4 82.99669 78.01292 135.01474  85.99827   4
5 66.99767 91.01036  88.99986  51.98974   5
6 79.00401 78.01292 113.52225 155.00644   6> > x <-
pivot_longer(input,names_to = "col",cols=1:12)> head(x, 20)# A tibble:
20 × 3
 row col   value
  1 1 ...1   93.0 2 1 ...2   35.0 3 1
...3   24.0 4 1 ...4   43.0 5 1 ...5   53.0 6 1 ...6
62.0 7 1 ...7   91.0 8 1 ...8   89.0 9 1 ...9   54.010
1 ...10  75.011 1 ...11 112. 12 1 ...12  49.013 2 ...1
76.014 2 ...2   22.015 2 ...3   22.016 2 ...4   25.017
2 ...5   45.018 2 ...6   61.019 2 ...7   63.020 2 ...8
93.0

>

Thanks

Jim Holtman
*Data Munger Guru*


*What is the problem that you are trying to solve?Tell me what you want to
do, not how you want to do it.*


On Fri, Oct 27, 2023 at 10:41 PM Paul Bernal  wrote:

> Hi Iris,
>
> Thank you so much for your valuable feedback. I wonder why your code gives
> you 1512 rows, given that the original structure has 12 columns and 126
> rows, so I would expect (125*12)+ 9=1,509 total rows.
>
> Cheers,
> Paul
> El El vie, 27 de oct. de 2023 a la(s) 10:40 p. m., Iris Simmons <
> ikwsi...@gmail.com> escribió:
>
> > You are not getting the structure you want because the indexes are
> > wrong. They should be something more like this:
> >
> > i <- 0
> > for (row in 1:nrow(alajuela_df)){
> >   for (col in 1:ncol(alajuela_df)){
> > i <- i + 1
> > df[i,1]=alajuela_df[row,col]
> >   }
> > }
> >
> > but I think what you are doing can be written much shorter and will run
> > faster:
> >
> > ## transpose here matches your original code
> > df <- data.frame(aportes_alajuela = c(t(alajuela_df)))
> >
> > ## but if you do not want to transpose, then do this
> > df <- data.frame(aportes_alajuela = unlist(alajuela_df, use.names =
> FALSE))
> >
> > However, you said you expected 1509 observations, but this gives you
> > 1512 observations. If you want to exclude the 3 NA observations, do
> > something like:
> >
> > df <- df[!is.na(df$aportes_alajuela), , drop = FALSE]
> >
> > On Fri, Oct 27, 2023 at 11:14 PM Paul Bernal 
> > wrote:
> > >
> > > Dear friends,
> > >
> > > I have the following dataframe:
> > > dim(alajuela_df)
> > > [1] 126  12
> > >
> > > dput(alajuela_df)
> > > structure(list(...1 = c(92.9925354, 76.0024254, 44.99547465,
> > > 28.00536465, 120.0068103, 31.9980405, 85.0071837, 40.1532933,
> > > 19.3120917, 113.12581575, 28.45843425, 114.400074, 143.925,
> > > 46.439634, 20.7845679, 50.82874575, 36.9818061, 44.6273556,
> 40.57804605,
> > > 30.38398005, 47.94042705, 36.38715225, 28.06199835, 28.4867511,
> > > 122.86681215, 56.4071652, 35.9057658, 52.669341, 24.94714485,
> > > 54.4249857, 61.164396, 47.88379335, 30.582198, 26.051502, 43.041612,
> > > 64.59073485, 51.6499344, 78.8202902886201, 35.2390173175627,
> > > 82.2394568898745, 47.760850180466, 54.3654763342294, 49.4878058854839,
> > > 32.8813266149642, 38.9301880693548, 51.9506275455197, 55.4404001992832,
> > > 50.7979761262545, 37.1198211413082, 36.9144309425627, 33.7829493281362,
> > > 32.8647492475806, 42.892686344, 63.9814428257048, 39.219040238172,
> > > 88.7557324417563, 42.0964144925627, 129.15973304991, 117.872998635484,
> > > 35.4004098300179, 83.4102757505377, 38.6443638074373, 100.491764259319,
> > > 40.219162961828, 35.901029409319, 85.2814714674731, 26.5821299974014,
> > > 33.0141992892473, 52.4006692386201, 62.7450310643369, 33.3732853655914,
> > > 36.4616827405018, 146.501706130466, 59.9869292767025, 132.659282967294,
> > > 29.9692075517921, 45.8436438112007, 29.3028312143369, 49.9042699517921,
> > > 27.467868886, 29.8483957580645, 26.1192323867384, 62.1371491517921,
> > > 53.92489050681, 64.5840869873656, 28.4476420455197, 52.9893180218638,
> > > 36.4730500781362, 40.8595060662186, 33.4571194806452, 35.6201445262545,
> > > 

Re: [R] How to Reformat a dataframe

2023-10-28 Thread avi.e.gross
Paul,

I have snipped away your long message and want to suggest another approach
or way of thinking to consider.

You have received other good suggestions and I likely would have used
something like that, probably within the dplyr/tidyverse but consider
something simpler.

You seem to be viewing a data.frame as similar to a matrix that you want to
reformat. There are similarities but a data.frame is also different. A
Matrix actually may be the right way for you to deal with your data. Can you
read it in as a matrix or must it be a data.frame? 

The thing about a matrix is that underneath, it is just a linear vector
which you really seem to want. All your columns seem to be the same kind of
numeric and perhaps the order does not matter whether it is row major or
column major. So consider my smaller example. I am making a data.frame that
is smaller for illustration:

> small <- data.frame(A=1:4, B=5:8, C=9:12)
> small
  A B  C
1 1 5  9
2 2 6 10
3 3 7 11
4 4 8 12

Now I am making it a matrix and keeping the columns the same:

> small.mat <- as.matrix(small)
> small.mat
 A B  C
[1,] 1 5  9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12

This can be linearized into a vector in many ways such as this:

> small.vec <- as.vector(small.mat)
> small.vec
 [1]  1  2  3  4  5  6  7  8  9 10 11 12

You can make that into a data.frame if you like:

> revised <- data.frame(colname=small.vec)
> revised
   colname
11
22
33
44
55
66
77
88
99
10  10
11  11
12  12

Of course, the above can be combined into more of a one-liner or made more
efficient. But in some cases, if you know the exact details of your
data.frame, you can spell out a way to combine the columns trivially. In my
example, I have three columns that can simply be concatenated into a vector
like so:

> small.onecol <- data.frame(onecol=c(small$A, small$B, small$C))
> small.onecol
   onecol
1   1
2   2
3   3
4   4
5   5
6   6
7   7
8   8
9   9
10 10
11 11
12 12

This is not a generalized solution but is simple enough even with the number
of columns you have. You are simply consolidating the vectors into one
bigger one. If you want to connect many, there are shorter loops that can do
it as in:

> cols <- colnames(small)
> cols
[1] "A" "B" "C"
> 
> new <- vector(mode="numeric", length=0)
> for (col in cols) {
+   new <- append(new, small[[col]])
+ }
> new
 [1]  1  2  3  4  5  6  7  8  9 10 11 12
> 
> new.df <- data.frame(newname=new)
> new.df
   newname
11
22
33
44
55
66
77
88
99
10  10
11  11
12  12

The number of ways to do what you want is huge. You can pick a way that
makes more sense to you, especially the ones others have supplied, or one
that seems more efficient. As noted, all methods may also need to deal with
your NA issue at some stage.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to Reformat a dataframe

2023-10-28 Thread Rui Barradas

Às 04:13 de 28/10/2023, Paul Bernal escreveu:

Dear friends,

I have the following dataframe:
dim(alajuela_df)
[1] 126  12

dput(alajuela_df)
structure(list(...1 = c(92.9925354, 76.0024254, 44.99547465,
28.00536465, 120.0068103, 31.9980405, 85.0071837, 40.1532933,
19.3120917, 113.12581575, 28.45843425, 114.400074, 143.925,
46.439634, 20.7845679, 50.82874575, 36.9818061, 44.6273556, 40.57804605,
30.38398005, 47.94042705, 36.38715225, 28.06199835, 28.4867511,
122.86681215, 56.4071652, 35.9057658, 52.669341, 24.94714485,
54.4249857, 61.164396, 47.88379335, 30.582198, 26.051502, 43.041612,
64.59073485, 51.6499344, 78.8202902886201, 35.2390173175627,
82.2394568898745, 47.760850180466, 54.3654763342294, 49.4878058854839,
32.8813266149642, 38.9301880693548, 51.9506275455197, 55.4404001992832,
50.7979761262545, 37.1198211413082, 36.9144309425627, 33.7829493281362,
32.8647492475806, 42.892686344, 63.9814428257048, 39.219040238172,
88.7557324417563, 42.0964144925627, 129.15973304991, 117.872998635484,
35.4004098300179, 83.4102757505377, 38.6443638074373, 100.491764259319,
40.219162961828, 35.901029409319, 85.2814714674731, 26.5821299974014,
33.0141992892473, 52.4006692386201, 62.7450310643369, 33.3732853655914,
36.4616827405018, 146.501706130466, 59.9869292767025, 132.659282967294,
29.9692075517921, 45.8436438112007, 29.3028312143369, 49.9042699517921,
27.467868886, 29.8483957580645, 26.1192323867384, 62.1371491517921,
53.92489050681, 64.5840869873656, 28.4476420455197, 52.9893180218638,
36.4730500781362, 40.8595060662186, 33.4571194806452, 35.6201445262545,
47.5373940344086, 62.5177012273297, 31.970248036, 29.6637272685484,
49.9578249978495, 26.1936613831541, 30.8051128442652, 153.165249601165,
36.7652008047491, 14.3854334390681, 55.9930862447133, 87.5882628859319,
88.414763059767, 58.6046644034946, 20.5301898890681, 39.0037205346774,
67.5971386298632, 25.1295804696237, 32.7864973072581, 59.5662701215054,
56.3503435331631, 25.2768483884409, 120.187620533964, 51.3699688189953,
42.6889983487868, 34.5183987020052, 32.9513272936733, 20.6141437461215,
43.4197954932283, 107.258904254306, 23.7548470509806, 48.4457090643702,
72.3249287581108, 32.7272590911596, 48.6115621286089), ...2 = c(34.9996266,
22.00219245, 22.9932822, 19.00060635, 35.99071635, 23.9560551,
44.99547465, 22.00219245, 17.35822905, 38.9639856, 16.4804067,
93.87035775, 81.2693595, 65.4119235, 14.6114946, 33.32893245,
23.3330844, 66.68618175, 29.1097218, 18.77407155, 28.1469489,
20.64298365, 15.31941585, 25.79665035, 39.5869563, 35.9057658,
28.11863205, 27.09922545, 18.03783345, 52.3295388, 47.99706075,
21.88892505, 18.8590221, 17.64139755, 29.2796229, 27.750513,
26.9576412, 36.8932410314484, 19.1194611744253, 37.5166237491071,
37.9625421619439, 23.444984650496, 37.4408609345785, 43.1033210084325,
28.1252430606151, 38.4216392645833, 41.0532302780651, 34.4822260610119,
27.8096188059524, 25.9720387760913, 19.5989670673372, 21.1366557174603,
34.0091174203373, 111.149568174301, 24.5438738482759, 62.923693488747,
36.056976280754, 36.934798630754, 47.0895472967433, 23.0987961767857,
45.1991799260913, 23.0274422095238, 26.8716961246169, 21.5009916962302,
25.315076619246, 41.2098751296627, 15.6537752838123, 20.1723471152778,
31.7437694203373, 34.6486999232143, 27.6318028338123, 23.9637897951389,
50.6646878095238, 33.3208231933532, 38.0673028746602, 19.9469359998016,
27.0503264451389, 19.838575356, 29.0062549955939, 17.231889453373,
25.332044256, 17.651735447619, 37.12988188841, 37.7982565469246,
33.1890524548611, 15.4283008803571, 33.5040593288314, 24.9726150325397,
29.0612348127976, 30.2629592267857, 32.8065534719349, 43.3676303365079,
39.19334780424, 23.9252474159722, 15.8641806903257, 27.51501019685,
21.5861832758362, 18.5136698379577, 54.9166790494253, 29.2865335598214,
12.8130786005824, 46.2078437446569, 37.9351942968391, 31.8758398080357,
25.483024048, 17.4062173291544, 19.1528843310516, 37.2160194109127,
25.3577400835684, 21.3381885368056, 37.5842732179795, 62.4856267757438,
19.2393143990079, 49.4806272500855, 29.5274325254668, 18.7819282827049,
19.5089694997422, 25.524181172185, 15.9275217745507, 25.461470312583,
37.4026236465872, 14.6120197959536, 26.5773978462589, 27.6541154512847,
22.5193597452548, 27.9919830179818), ...3 = c(24.04100565, 22.00219245,
15.0079305, 12.99743415, 22.9932822, 13.9885239, 27.0142749,
16.3671393, 12.2895129, 28.68496905, 16.56535725, 32.70596175,
46.01488125, 24.94714485, 10.8170367, 19.68021075, 15.0079305,
26.41962105, 21.9738756, 15.71585175, 21.26595435, 13.28060265,
11.6099085, 15.5742675, 25.96655145, 20.5013994, 15.99902025,
18.03783345, 11.07188835, 32.59269435, 29.7326925, 15.82911915,
13.4788206, 22.11545985, 17.6130807, 22.3136778, 17.01842685,
24.8211230265233, 13.237941302509, 22.3448026530466, 26.816253605,
19.755485530466, 28.9524567137097, 35.9159180773958, 29.0510074712366,
29.6768770591437, 19.74645255681, 21.3046743438172, 18.2600040043011,
14.1888055630824, 13.2245440831541, 

Re: [R] How to Reformat a dataframe

2023-10-28 Thread Chris Evans via R-help

The tidyverse idiom looks very different but does what you want and I have come 
to like it.
What idiom of R one likes, for the mostly small datasets I handle, is largely a 
matter
of preferenceds for "readability", itself very personal.  Here's my tidyverse 
way of doing
what you wanted:

### start of code
library(tidyverse)
# tmpDF <- structure(list(...1 = c(92.9925354, 76.0024254, 44.99547465,
### I omitted the rest of reconstructing the dataframe for brevity, easy to 
reconstruct

tmpDF %>%
  pivot_longer(cols = everything()) -> tmpTibLong

tmpTibLong
### showed:
# # A tibble: 1,512 × 2
# name  value
#  
#   1 ...1   93.0
# 2 ...2   35.0
# 3 ...3   24.0
# 4 ...4   43.0
# 5 ...5   53.0
# 6 ...6   62.0
# 7 ...7   91.0
# 8 ...8   89.0
# 9 ...9   54.0
# 10 ...10  75.0
# # ℹ 1,502 more rows
# # ℹ Use `print(n = ...)` to see more rows

### as you don't want the missing values
tmpTibLong %>%
  drop_na()
### gave:
# # A tibble: 1,509 × 2
# name  value
#  
#   1 ...1   93.0
# 2 ...2   35.0
# 3 ...3   24.0
# 4 ...4   43.0
# 5 ...5   53.0
# 6 ...6   62.0
# 7 ...7   91.0
# 8 ...8   89.0
# 9 ...9   54.0
# 10 ...10  75.0
# # ℹ 1,499 more rows
# # ℹ Use `print(n = ...)` to see more rows
### end

Very best all,

Chris

On 28/10/2023 07:41, Paul Bernal wrote:


Hi Iris,

Thank you so much for your valuable feedback. I wonder why your code gives
you 1512 rows, given that the original structure has 12 columns and 126
rows, so I would expect (125*12)+ 9=1,509 total rows.

Cheers,
Paul
El El vie, 27 de oct. de 2023 a la(s) 10:40 p. m., Iris Simmons <
ikwsi...@gmail.com> escribió:


You are not getting the structure you want because the indexes are
wrong. They should be something more like this:

i <- 0
for (row in 1:nrow(alajuela_df)){
   for (col in 1:ncol(alajuela_df)){
 i <- i + 1
 df[i,1]=alajuela_df[row,col]
   }
}

but I think what you are doing can be written much shorter and will run
faster:

## transpose here matches your original code
df <- data.frame(aportes_alajuela = c(t(alajuela_df)))

## but if you do not want to transpose, then do this
df <- data.frame(aportes_alajuela = unlist(alajuela_df, use.names = FALSE))

However, you said you expected 1509 observations, but this gives you
1512 observations. If you want to exclude the 3 NA observations, do
something like:

df <- df[!is.na(df$aportes_alajuela), , drop = FALSE]

On Fri, Oct 27, 2023 at 11:14 PM Paul Bernal 
wrote:

Dear friends,

I have the following dataframe:
dim(alajuela_df)
[1] 126  12


[dput snipped]



What I want to do is, instead of having 12 observations  by row, I want

to

have one observation by row. I want to have a single column with 1509
observations instead of 126 rows with 12 columns per row.

I tried the following:
df = data.frame(matrix(nrow = Length, ncol = 1))
colnames(df) = c("aportes_alajuela")



for (row in 1:nrow(alajuela_df)){
   for (col in 1:ncol(alajuela_df)){
 df[i,1]=alajuela_df[i,j]
   }
}

But I am not getting the data in the structure I want.

Any help will be greatly appreciated.

Best regards,
Paul

 [[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide

http://www.R-project.org/posting-guide.html

and provide commented, minimal, self-contained, reproducible code.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

--
Chris Evans (he/him)
Visiting Professor, UDLA, Quito, Ecuador & Honorary Professor, 
University of Roehampton, London, UK.

Work web site: https://www.psyctc.org/psyctc/
CORE site: http://www.coresystemtrust.org.uk/
Personal site: https://www.psyctc.org/pelerinage2016/
Emeetings (Thursdays): 
https://www.psyctc.org/psyctc/booking-meetings-with-me/

(Beware: French time, generally an hour ahead of UK)


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to Reformat a dataframe

2023-10-27 Thread Paul Bernal
Hi Iris,

Thank you so much for your valuable feedback. I wonder why your code gives
you 1512 rows, given that the original structure has 12 columns and 126
rows, so I would expect (125*12)+ 9=1,509 total rows.

Cheers,
Paul
El El vie, 27 de oct. de 2023 a la(s) 10:40 p. m., Iris Simmons <
ikwsi...@gmail.com> escribió:

> You are not getting the structure you want because the indexes are
> wrong. They should be something more like this:
>
> i <- 0
> for (row in 1:nrow(alajuela_df)){
>   for (col in 1:ncol(alajuela_df)){
> i <- i + 1
> df[i,1]=alajuela_df[row,col]
>   }
> }
>
> but I think what you are doing can be written much shorter and will run
> faster:
>
> ## transpose here matches your original code
> df <- data.frame(aportes_alajuela = c(t(alajuela_df)))
>
> ## but if you do not want to transpose, then do this
> df <- data.frame(aportes_alajuela = unlist(alajuela_df, use.names = FALSE))
>
> However, you said you expected 1509 observations, but this gives you
> 1512 observations. If you want to exclude the 3 NA observations, do
> something like:
>
> df <- df[!is.na(df$aportes_alajuela), , drop = FALSE]
>
> On Fri, Oct 27, 2023 at 11:14 PM Paul Bernal 
> wrote:
> >
> > Dear friends,
> >
> > I have the following dataframe:
> > dim(alajuela_df)
> > [1] 126  12
> >
> > dput(alajuela_df)
> > structure(list(...1 = c(92.9925354, 76.0024254, 44.99547465,
> > 28.00536465, 120.0068103, 31.9980405, 85.0071837, 40.1532933,
> > 19.3120917, 113.12581575, 28.45843425, 114.400074, 143.925,
> > 46.439634, 20.7845679, 50.82874575, 36.9818061, 44.6273556, 40.57804605,
> > 30.38398005, 47.94042705, 36.38715225, 28.06199835, 28.4867511,
> > 122.86681215, 56.4071652, 35.9057658, 52.669341, 24.94714485,
> > 54.4249857, 61.164396, 47.88379335, 30.582198, 26.051502, 43.041612,
> > 64.59073485, 51.6499344, 78.8202902886201, 35.2390173175627,
> > 82.2394568898745, 47.760850180466, 54.3654763342294, 49.4878058854839,
> > 32.8813266149642, 38.9301880693548, 51.9506275455197, 55.4404001992832,
> > 50.7979761262545, 37.1198211413082, 36.9144309425627, 33.7829493281362,
> > 32.8647492475806, 42.892686344, 63.9814428257048, 39.219040238172,
> > 88.7557324417563, 42.0964144925627, 129.15973304991, 117.872998635484,
> > 35.4004098300179, 83.4102757505377, 38.6443638074373, 100.491764259319,
> > 40.219162961828, 35.901029409319, 85.2814714674731, 26.5821299974014,
> > 33.0141992892473, 52.4006692386201, 62.7450310643369, 33.3732853655914,
> > 36.4616827405018, 146.501706130466, 59.9869292767025, 132.659282967294,
> > 29.9692075517921, 45.8436438112007, 29.3028312143369, 49.9042699517921,
> > 27.467868886, 29.8483957580645, 26.1192323867384, 62.1371491517921,
> > 53.92489050681, 64.5840869873656, 28.4476420455197, 52.9893180218638,
> > 36.4730500781362, 40.8595060662186, 33.4571194806452, 35.6201445262545,
> > 47.5373940344086, 62.5177012273297, 31.970248036, 29.6637272685484,
> > 49.9578249978495, 26.1936613831541, 30.8051128442652, 153.165249601165,
> > 36.7652008047491, 14.3854334390681, 55.9930862447133, 87.5882628859319,
> > 88.414763059767, 58.6046644034946, 20.5301898890681, 39.0037205346774,
> > 67.5971386298632, 25.1295804696237, 32.7864973072581, 59.5662701215054,
> > 56.3503435331631, 25.2768483884409, 120.187620533964, 51.3699688189953,
> > 42.6889983487868, 34.5183987020052, 32.9513272936733, 20.6141437461215,
> > 43.4197954932283, 107.258904254306, 23.7548470509806, 48.4457090643702,
> > 72.3249287581108, 32.7272590911596, 48.6115621286089), ...2 =
> c(34.9996266,
> > 22.00219245, 22.9932822, 19.00060635, 35.99071635, 23.9560551,
> > 44.99547465, 22.00219245, 17.35822905, 38.9639856, 16.4804067,
> > 93.87035775, 81.2693595, 65.4119235, 14.6114946, 33.32893245,
> > 23.3330844, 66.68618175, 29.1097218, 18.77407155, 28.1469489,
> > 20.64298365, 15.31941585, 25.79665035, 39.5869563, 35.9057658,
> > 28.11863205, 27.09922545, 18.03783345, 52.3295388, 47.99706075,
> > 21.88892505, 18.8590221, 17.64139755, 29.2796229, 27.750513,
> > 26.9576412, 36.8932410314484, 19.1194611744253, 37.5166237491071,
> > 37.9625421619439, 23.444984650496, 37.4408609345785, 43.1033210084325,
> > 28.1252430606151, 38.4216392645833, 41.0532302780651, 34.4822260610119,
> > 27.8096188059524, 25.9720387760913, 19.5989670673372, 21.1366557174603,
> > 34.0091174203373, 111.149568174301, 24.5438738482759, 62.923693488747,
> > 36.056976280754, 36.934798630754, 47.0895472967433, 23.0987961767857,
> > 45.1991799260913, 23.0274422095238, 26.8716961246169, 21.5009916962302,
> > 25.315076619246, 41.2098751296627, 15.6537752838123, 20.1723471152778,
> > 31.7437694203373, 34.6486999232143, 27.6318028338123, 23.9637897951389,
> > 50.6646878095238, 33.3208231933532, 38.0673028746602, 19.9469359998016,
> > 27.0503264451389, 19.838575356, 29.0062549955939, 17.231889453373,
> > 25.332044256, 17.651735447619, 37.12988188841, 37.7982565469246,
> > 33.1890524548611, 15.4283008803571, 33.5040593288314, 24.9726150325397,
> > 29.0612348127976, 

Re: [R] How to Reformat a dataframe

2023-10-27 Thread Iris Simmons
You are not getting the structure you want because the indexes are
wrong. They should be something more like this:

i <- 0
for (row in 1:nrow(alajuela_df)){
  for (col in 1:ncol(alajuela_df)){
i <- i + 1
df[i,1]=alajuela_df[row,col]
  }
}

but I think what you are doing can be written much shorter and will run faster:

## transpose here matches your original code
df <- data.frame(aportes_alajuela = c(t(alajuela_df)))

## but if you do not want to transpose, then do this
df <- data.frame(aportes_alajuela = unlist(alajuela_df, use.names = FALSE))

However, you said you expected 1509 observations, but this gives you
1512 observations. If you want to exclude the 3 NA observations, do
something like:

df <- df[!is.na(df$aportes_alajuela), , drop = FALSE]

On Fri, Oct 27, 2023 at 11:14 PM Paul Bernal  wrote:
>
> Dear friends,
>
> I have the following dataframe:
> dim(alajuela_df)
> [1] 126  12
>
> dput(alajuela_df)
> structure(list(...1 = c(92.9925354, 76.0024254, 44.99547465,
> 28.00536465, 120.0068103, 31.9980405, 85.0071837, 40.1532933,
> 19.3120917, 113.12581575, 28.45843425, 114.400074, 143.925,
> 46.439634, 20.7845679, 50.82874575, 36.9818061, 44.6273556, 40.57804605,
> 30.38398005, 47.94042705, 36.38715225, 28.06199835, 28.4867511,
> 122.86681215, 56.4071652, 35.9057658, 52.669341, 24.94714485,
> 54.4249857, 61.164396, 47.88379335, 30.582198, 26.051502, 43.041612,
> 64.59073485, 51.6499344, 78.8202902886201, 35.2390173175627,
> 82.2394568898745, 47.760850180466, 54.3654763342294, 49.4878058854839,
> 32.8813266149642, 38.9301880693548, 51.9506275455197, 55.4404001992832,
> 50.7979761262545, 37.1198211413082, 36.9144309425627, 33.7829493281362,
> 32.8647492475806, 42.892686344, 63.9814428257048, 39.219040238172,
> 88.7557324417563, 42.0964144925627, 129.15973304991, 117.872998635484,
> 35.4004098300179, 83.4102757505377, 38.6443638074373, 100.491764259319,
> 40.219162961828, 35.901029409319, 85.2814714674731, 26.5821299974014,
> 33.0141992892473, 52.4006692386201, 62.7450310643369, 33.3732853655914,
> 36.4616827405018, 146.501706130466, 59.9869292767025, 132.659282967294,
> 29.9692075517921, 45.8436438112007, 29.3028312143369, 49.9042699517921,
> 27.467868886, 29.8483957580645, 26.1192323867384, 62.1371491517921,
> 53.92489050681, 64.5840869873656, 28.4476420455197, 52.9893180218638,
> 36.4730500781362, 40.8595060662186, 33.4571194806452, 35.6201445262545,
> 47.5373940344086, 62.5177012273297, 31.970248036, 29.6637272685484,
> 49.9578249978495, 26.1936613831541, 30.8051128442652, 153.165249601165,
> 36.7652008047491, 14.3854334390681, 55.9930862447133, 87.5882628859319,
> 88.414763059767, 58.6046644034946, 20.5301898890681, 39.0037205346774,
> 67.5971386298632, 25.1295804696237, 32.7864973072581, 59.5662701215054,
> 56.3503435331631, 25.2768483884409, 120.187620533964, 51.3699688189953,
> 42.6889983487868, 34.5183987020052, 32.9513272936733, 20.6141437461215,
> 43.4197954932283, 107.258904254306, 23.7548470509806, 48.4457090643702,
> 72.3249287581108, 32.7272590911596, 48.6115621286089), ...2 = c(34.9996266,
> 22.00219245, 22.9932822, 19.00060635, 35.99071635, 23.9560551,
> 44.99547465, 22.00219245, 17.35822905, 38.9639856, 16.4804067,
> 93.87035775, 81.2693595, 65.4119235, 14.6114946, 33.32893245,
> 23.3330844, 66.68618175, 29.1097218, 18.77407155, 28.1469489,
> 20.64298365, 15.31941585, 25.79665035, 39.5869563, 35.9057658,
> 28.11863205, 27.09922545, 18.03783345, 52.3295388, 47.99706075,
> 21.88892505, 18.8590221, 17.64139755, 29.2796229, 27.750513,
> 26.9576412, 36.8932410314484, 19.1194611744253, 37.5166237491071,
> 37.9625421619439, 23.444984650496, 37.4408609345785, 43.1033210084325,
> 28.1252430606151, 38.4216392645833, 41.0532302780651, 34.4822260610119,
> 27.8096188059524, 25.9720387760913, 19.5989670673372, 21.1366557174603,
> 34.0091174203373, 111.149568174301, 24.5438738482759, 62.923693488747,
> 36.056976280754, 36.934798630754, 47.0895472967433, 23.0987961767857,
> 45.1991799260913, 23.0274422095238, 26.8716961246169, 21.5009916962302,
> 25.315076619246, 41.2098751296627, 15.6537752838123, 20.1723471152778,
> 31.7437694203373, 34.6486999232143, 27.6318028338123, 23.9637897951389,
> 50.6646878095238, 33.3208231933532, 38.0673028746602, 19.9469359998016,
> 27.0503264451389, 19.838575356, 29.0062549955939, 17.231889453373,
> 25.332044256, 17.651735447619, 37.12988188841, 37.7982565469246,
> 33.1890524548611, 15.4283008803571, 33.5040593288314, 24.9726150325397,
> 29.0612348127976, 30.2629592267857, 32.8065534719349, 43.3676303365079,
> 39.19334780424, 23.9252474159722, 15.8641806903257, 27.51501019685,
> 21.5861832758362, 18.5136698379577, 54.9166790494253, 29.2865335598214,
> 12.8130786005824, 46.2078437446569, 37.9351942968391, 31.8758398080357,
> 25.483024048, 17.4062173291544, 19.1528843310516, 37.2160194109127,
> 25.3577400835684, 21.3381885368056, 37.5842732179795, 62.4856267757438,
> 19.2393143990079, 49.4806272500855, 29.5274325254668, 

[R] How to Reformat a dataframe

2023-10-27 Thread Paul Bernal
Dear friends,

I have the following dataframe:
dim(alajuela_df)
[1] 126  12

dput(alajuela_df)
structure(list(...1 = c(92.9925354, 76.0024254, 44.99547465,
28.00536465, 120.0068103, 31.9980405, 85.0071837, 40.1532933,
19.3120917, 113.12581575, 28.45843425, 114.400074, 143.925,
46.439634, 20.7845679, 50.82874575, 36.9818061, 44.6273556, 40.57804605,
30.38398005, 47.94042705, 36.38715225, 28.06199835, 28.4867511,
122.86681215, 56.4071652, 35.9057658, 52.669341, 24.94714485,
54.4249857, 61.164396, 47.88379335, 30.582198, 26.051502, 43.041612,
64.59073485, 51.6499344, 78.8202902886201, 35.2390173175627,
82.2394568898745, 47.760850180466, 54.3654763342294, 49.4878058854839,
32.8813266149642, 38.9301880693548, 51.9506275455197, 55.4404001992832,
50.7979761262545, 37.1198211413082, 36.9144309425627, 33.7829493281362,
32.8647492475806, 42.892686344, 63.9814428257048, 39.219040238172,
88.7557324417563, 42.0964144925627, 129.15973304991, 117.872998635484,
35.4004098300179, 83.4102757505377, 38.6443638074373, 100.491764259319,
40.219162961828, 35.901029409319, 85.2814714674731, 26.5821299974014,
33.0141992892473, 52.4006692386201, 62.7450310643369, 33.3732853655914,
36.4616827405018, 146.501706130466, 59.9869292767025, 132.659282967294,
29.9692075517921, 45.8436438112007, 29.3028312143369, 49.9042699517921,
27.467868886, 29.8483957580645, 26.1192323867384, 62.1371491517921,
53.92489050681, 64.5840869873656, 28.4476420455197, 52.9893180218638,
36.4730500781362, 40.8595060662186, 33.4571194806452, 35.6201445262545,
47.5373940344086, 62.5177012273297, 31.970248036, 29.6637272685484,
49.9578249978495, 26.1936613831541, 30.8051128442652, 153.165249601165,
36.7652008047491, 14.3854334390681, 55.9930862447133, 87.5882628859319,
88.414763059767, 58.6046644034946, 20.5301898890681, 39.0037205346774,
67.5971386298632, 25.1295804696237, 32.7864973072581, 59.5662701215054,
56.3503435331631, 25.2768483884409, 120.187620533964, 51.3699688189953,
42.6889983487868, 34.5183987020052, 32.9513272936733, 20.6141437461215,
43.4197954932283, 107.258904254306, 23.7548470509806, 48.4457090643702,
72.3249287581108, 32.7272590911596, 48.6115621286089), ...2 = c(34.9996266,
22.00219245, 22.9932822, 19.00060635, 35.99071635, 23.9560551,
44.99547465, 22.00219245, 17.35822905, 38.9639856, 16.4804067,
93.87035775, 81.2693595, 65.4119235, 14.6114946, 33.32893245,
23.3330844, 66.68618175, 29.1097218, 18.77407155, 28.1469489,
20.64298365, 15.31941585, 25.79665035, 39.5869563, 35.9057658,
28.11863205, 27.09922545, 18.03783345, 52.3295388, 47.99706075,
21.88892505, 18.8590221, 17.64139755, 29.2796229, 27.750513,
26.9576412, 36.8932410314484, 19.1194611744253, 37.5166237491071,
37.9625421619439, 23.444984650496, 37.4408609345785, 43.1033210084325,
28.1252430606151, 38.4216392645833, 41.0532302780651, 34.4822260610119,
27.8096188059524, 25.9720387760913, 19.5989670673372, 21.1366557174603,
34.0091174203373, 111.149568174301, 24.5438738482759, 62.923693488747,
36.056976280754, 36.934798630754, 47.0895472967433, 23.0987961767857,
45.1991799260913, 23.0274422095238, 26.8716961246169, 21.5009916962302,
25.315076619246, 41.2098751296627, 15.6537752838123, 20.1723471152778,
31.7437694203373, 34.6486999232143, 27.6318028338123, 23.9637897951389,
50.6646878095238, 33.3208231933532, 38.0673028746602, 19.9469359998016,
27.0503264451389, 19.838575356, 29.0062549955939, 17.231889453373,
25.332044256, 17.651735447619, 37.12988188841, 37.7982565469246,
33.1890524548611, 15.4283008803571, 33.5040593288314, 24.9726150325397,
29.0612348127976, 30.2629592267857, 32.8065534719349, 43.3676303365079,
39.19334780424, 23.9252474159722, 15.8641806903257, 27.51501019685,
21.5861832758362, 18.5136698379577, 54.9166790494253, 29.2865335598214,
12.8130786005824, 46.2078437446569, 37.9351942968391, 31.8758398080357,
25.483024048, 17.4062173291544, 19.1528843310516, 37.2160194109127,
25.3577400835684, 21.3381885368056, 37.5842732179795, 62.4856267757438,
19.2393143990079, 49.4806272500855, 29.5274325254668, 18.7819282827049,
19.5089694997422, 25.524181172185, 15.9275217745507, 25.461470312583,
37.4026236465872, 14.6120197959536, 26.5773978462589, 27.6541154512847,
22.5193597452548, 27.9919830179818), ...3 = c(24.04100565, 22.00219245,
15.0079305, 12.99743415, 22.9932822, 13.9885239, 27.0142749,
16.3671393, 12.2895129, 28.68496905, 16.56535725, 32.70596175,
46.01488125, 24.94714485, 10.8170367, 19.68021075, 15.0079305,
26.41962105, 21.9738756, 15.71585175, 21.26595435, 13.28060265,
11.6099085, 15.5742675, 25.96655145, 20.5013994, 15.99902025,
18.03783345, 11.07188835, 32.59269435, 29.7326925, 15.82911915,
13.4788206, 22.11545985, 17.6130807, 22.3136778, 17.01842685,
24.8211230265233, 13.237941302509, 22.3448026530466, 26.816253605,
19.755485530466, 28.9524567137097, 35.9159180773958, 29.0510074712366,
29.6768770591437, 19.74645255681, 21.3046743438172, 18.2600040043011,
14.1888055630824, 13.2245440831541, 14.2163781231183, 19.9802103525986,