[R] split data into columns from dataframe

Rui Barradas ruipbarradas at sapo.pt
Tue Mar 12 11:33:22 CET 2013


Hello,

Like Ivan said, use read.table to read in the data and then, to select 
only some of the columns you can use several ways.


data_30min <- read.table(text = "
1  19710629 080000(PARTIAL)       39.3 at interval beginning 19701126 010326
2  19720629 080000(PARTIAL)       33.8 at interval beginning 19720517 144507
3  19730629 080000(PARTIAL)       32.2 at interval beginning 19720910 135747
4  19740629 080000(PARTIAL)       38.9 at interval beginning 19731003 124849
5  19750629 080000                43.3 at interval beginning 19750105 173500
6  19760629 080000(PARTIAL)      101.5 at interval beginning 19750809 111800
7  19770629 080000(PARTIAL)       39.4 at interval beginning 19760917 141200
8  19780629 080000                38.5 at interval beginning 19780508 195400
9  19790629 080000(PARTIAL)       39.0 at interval beginning 19790602 142222
10 19800629 080000(PARTIAL)       94.6 at interval beginning 19800329 063532
")

# Simple
data_30min[, c(2, 4, 8)]

# More elaborate, but equivalent
subset(data_30min, select = c("V2", "V4", "V8"))



Hope this helps,

Rui Barradas

Em 12-03-2013 09:03, Roslina Zakaria escreveu:
> Dear r-users,
>
> Originally my data is in notepad.  I use data.frame(...) to convert the data into columns but it has no header.  The data below is what I got in R.  I would like to extract these data:
>
> 19710629        39.3  19701126
> 19720629        33.8  19720517
> ...
> when I use data2_30min[1, cbind(1,3,5)] command the result is NULL.
>
> Then I tried data2_30min[1], it gives me the whole set of data.  I suspect the data is stored in a cell.
>
> My question is, how do I split the data from dataframe into some columns which separating the data by spaces.  Many examples given on how to split the data with the header.
>
> The codes and data are given below.  Thank you so much for your help.
>
> head(data_30min,10); tail(data_30min,10)
> data2_30min <- data.frame(data_30min)
> head(data2_30min,10); tail(data2_30min,10)
> head(data_30min,10); tail(data_30min,10)
> data2_30min[1]
>
>                                                                    data_30min
> 1  19710629 080000(PARTIAL)       39.3 at interval beginning 19701126 010326
> 2  19720629 080000(PARTIAL)       33.8 at interval beginning 19720517 144507
> 3  19730629 080000(PARTIAL)       32.2 at interval beginning 19720910 135747
> 4  19740629 080000(PARTIAL)       38.9 at interval beginning 19731003 124849
> 5  19750629 080000                43.3 at interval beginning 19750105 173500
> 6  19760629 080000(PARTIAL)      101.5 at interval beginning 19750809 111800
> 7  19770629 080000(PARTIAL)       39.4 at interval beginning 19760917 141200
> 8  19780629 080000                38.5 at interval beginning 19780508 195400
> 9  19790629 080000(PARTIAL)       39.0 at interval beginning 19790602 142222
> 10 19800629 080000(PARTIAL)       94.6 at interval beginning 19800329 063532
>
> 	[[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



More information about the R-help mailing list