Retrieving a row from a data frame

I have a data frame in R that looks like this:

head(span_data)   

FECHA....DIA.Cá01TMax.Cá01HTMax.Cá01TMin.Cá01HTMin.Cá01TMed.Cá01HumMax.Cá01HumMin.Cá01HumMed.Cá01VelViento.Cá01DirViento.Cá01Rad.Cá01Precip.Cá01ETo
1 -------- --- -------- --------- -------- --------- -------- ---------- ---------- ---   ------- ------------- ------------- ------- ---------- ------- 
2  21-05-12 142     21.0     15:08      9.1      5:28     15.3       91.9       45.2        72.3           2.2         270.2    30.0        0.0    4.81
3  20-05-12 141     19.1     15:12     11.3      4:50     14.6       94.9       46.6       74.4           2.6         273.0    23.2       12.6     4.0
4  19-05-12 140     22.6     14:26     14.8     23:50     18.5       92.6       36.3       66.5           3.7         250.1    24.9        0.4    5.29
5  18-05-12 139     23.4     14:30     17.2     23:58     19.4       87.4       55.5       72.0           3.1         218.5    24.2        0.0    4.75
6  17-05-12 138     31.2     13:08     13.9      5:32     22.4       78.5       26.7       51.0           2.3         164.9    23.6        0.0    6.36

Right now, all strings are one long string, and I will convert them to numbers. However, when I want to extract one of the lines, I get

span_data[3,1]
[1] 20-05-12 141     19.1     15:12     11.3      4:50     14.6       94.9       46.6       74.4           2.6         273.0    23.2       12.6     4.0
4272 Levels: -------- --- -------- --------- -------- --------- -------- ---------- ---------- ---------- ------------- ------------- ------- ---------- -------  ...

I don’t need the “Levels” part. How to extract only a string? (I’m sure that this question has already been answered, but I didn’t know exactly how to ask the question.)

+5
source share
3 answers

may try ...

a <- c("1-1","2-1","3-1")
b <- 1:3
ab<-as.data.frame(cbind(a,b))

x <- ab[3,1] # what you don't want 
x
#[1] 3-1
#Levels: 1-1 2-1 3-1

z <- as.character(ab[3,1]) # without levels as it is no longer a factor
z   
#[1] "3-1"
+5
source

You can try the following:

 newdat <- read.table(text=span_data[[1]], stringsAsFactors=FALSE)
+2
source

It is best to skip the first 3 (?) Lines and provide the appropriate colClasses argument for read.table. But you can get what you want from what you have quite easily:

as.character(span_data[3,1])
0
source

All Articles