Split dataframe in objects

Hello everyone.

I trying to read a CSV Data to create some object.

For exemple: I need T9, T8, T7 as a vector with 10 positions, each position represents an year.

Anyone can help me?

I usually read a csv like this:

library(readr)

mydata <- read_csv("filename.csv")

Once you have done this you can do

dput(head(mydata))

to show us what the data looks like, and then explain what you want to do with it.

the dput function returned this:

structure(list(Nome = c("T3", "T4", "T9", "T2", "T5", "T11"), 
    `2010` = c(4617736, 6824024, 5026849, 4433242, 4568413, 5093264
    ), `2011` = c(4582520, 6795516, 5218895, 4457319, 4595475, 
    5430040), `2012` = c(4227291, 6330966, 4819989, 4105146, 
    4271776, 5760444), `2013` = c(4355989, 6605463, 5061965, 
    4000968, 4317409, 6334579), `2014` = c(4093088, 5981299, 
    4797879, 3172811, 3940792, 5570819), `2015` = c(3974123, 
    5912972, 4914931, 3228779, 3898327, 5787128), `2016` = c(3974675, 
    5736459, 4738498, 3313143, 3849244, 5916668), `2017` = c(3393483, 
    5128937, 4104066, 2984942, 3341371, 5497001), `2018` = c(3110618, 
    4727141, 3628676, 2878428, 2908832, 5305740), `2019` = c(2798186, 
    4416586, 3223225, 2709814, 2520034, 5185352)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame")) ``` 

I'd like to "split" the data. I need that "T1", "T2","T3" [...] be vectors with 10 positions.

I read the CSV Archiv with readr function too

Thanks for helping

You can transpose the data like this. I personally would leave them all in one dataframe.

df <- structure(list(Nome = c("T3", "T4", "T9", "T2", "T5", "T11"), 
`2010` = c(4617736, 6824024, 5026849, 4433242, 4568413, 5093264
), `2011` = c(4582520, 6795516, 5218895, 4457319, 4595475, 
5430040), `2012` = c(4227291, 6330966, 4819989, 4105146, 
4271776, 5760444), `2013` = c(4355989, 6605463, 5061965, 
4000968, 4317409, 6334579), `2014` = c(4093088, 5981299, 
4797879, 3172811, 3940792, 5570819), `2015` = c(3974123, 
5912972, 4914931, 3228779, 3898327, 5787128), `2016` = c(3974675, 
5736459, 4738498, 3313143, 3849244, 5916668), `2017` = c(3393483, 
5128937, 4104066, 2984942, 3341371, 5497001), `2018` = c(3110618, 
4727141, 3628676, 2878428, 2908832, 5305740), `2019` = c(2798186, 
4416586, 3223225, 2709814, 2520034, 5185352)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame")) 

newdf <- as.data.frame(t(df[, -1])) # transpose data except first column, make df
names(newdf) <- df$Nome # set column names

Thank you! I got it!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.