Operating data frame R

1. Insert a

Operates in accordance with the data set comes beaver, such as inserting an id.

> colnames(beaver1)
[1] "day"   "time"  "temp"  "activ"
> nrow(beaver1)
[1] 114

 method 1:

new_beaver1$id = rep(1,114)

 Method 2

new_beaver1 = data.frame(id = rep(1,114),beaver1)

 Method 3

x = data.frame(
  id = rep(1,114)
)
new_beaver1 = cbind(beaver1,x)

 

2. Insert a row

new_beaver1 = beaver1
x=c(348,350,98.15,0,1)
rbind(beaver1,x)

 

First clear beaver1 entire data frame is output, beaver1 [row range, the range column] to print out the data block specified range.

3. The extracted data in accordance with the conditions

beavear1 [OK condition, the condition column]

 

4. Delete data box data

Processing rules to delete the row or column is not given then to display a new data frame.

beaver1 [row, column]

For example beaver1 [- c (1: 5), 1: 2] to not print the first print line 5 and only 1 to 2.

 

5. A Problem:

beaver1 beaver2 and temperature data set contains two data beaver. Id add a column named as beaver1 data set, which values ​​are all 1. Similarly, to add a column for the id beaver2, the full value of 2. Splicing two vertical frame data, and find all active in a subset of the beaver.

> new_beaver1 = data.frame(id = rep(1,114),beaver1)
> new_beaver2 = data.frame(id = rep(2,100),beaver2)
> new_beaver=rbind(new_beaver1,new_beaver2)
> new_beaver[new_beaver$activ==1,]
    id day time  temp activ
54   1 346 1730 37.07     1
68   1 346 1950 37.10     1
80   1 346 2150 37.53     1
83   1 346 2230 37.25     1
86   1 346 2300 37.24     1
114  1 347  340 37.15     1
153  2 307 1550 37.98     1
154  2 307 1600 38.02     1
155  2 307 1610 38.00     1
156  2 307 1620 38.24     1
157  2 307 1630 38.10     1
158  2 307 1640 38.24     1
159  2 307 1650 38.11     1
160  2 307 1700 38.02     1
161  2 307 1710 38.11     1
162  2 307 1720 38.01     1
163  2 307 1730 37.91     1
164  2 307 1740 37.96     1
165  2 307 1750 38.03     1
166  2 307 1800 38.17     1
167  2 307 1810 38.19     1
168  2 307 1820 38.18     1
169  2 307 1830 38.15     1
170  2 307 1840 38.04     1
171  2 307 1850 37.96     1
172  2 307 1900 37.84     1
173  2 307 1910 37.83     1
174  2 307 1920 37.84     1
175  2 307 1930 37.74     1
176  2 307 1940 37.76     1
177  2 307 1950 37.76     1
178  2 307 2000 37.64     1
179  2 307 2010 37.63     1
180  2 307 2020 38.06     1
181  2 307 2030 38.19     1
182  2 307 2040 38.35     1
183  2 307 2050 38.25     1
184  2 307 2100 37.86     1
185  2 307 2110 37.95     1
186  2 307 2120 37.95     1
187  2 307 2130 37.76     1
188  2 307 2140 37.60     1
189  2 307 2150 37.89     1
190  2 307 2200 37.86     1
191  2 307 2210 37.71     1
192  2 307 2220 37.78     1
193  2 307 2230 37.82     1
194  2 307 2240 37.76     1
195  2 307 2250 37.81     1
196  2 307 2300 37.84     1
197  2 307 2310 38.01     1
198  2 307 2320 38.10     1
199  2 307 2330 38.15     1
200  2 307 2340 37.92     1
201  2 307 2350 37.64     1
202  2 308    0 37.70     1
203  2 308   10 37.46     1
204  2 308   20 37.41     1
205  2 308   30 37.46     1
206  2 308   40 37.56     1
207  2 308   50 37.55     1
208  2 308  100 37.75     1
209  2 308  110 37.76     1
210  2 308  120 37.73     1
211  2 308  130 37.77     1
212  2 308  140 38.01     1
213  2 308  150 38.04     1
214  2 308  200 38.07     1

  

 

Guess you like

Origin www.cnblogs.com/jiaxinwei/p/11517598.html