简体   繁体   中英

How to create a function, get input from a data frame and output to a text file using R?

I need to create a function called CarPFunction that will take CarP (data frame below) as input and store on separate lines, tab-delimited the manufacturer , model and year of the cars that use more than 20 gallons gas in the city in a .txt file.

manufacturer               model displ year cyl      trans drv cty hwy
        audi                  a4   1.8 1999   4   auto(l5)   f  18  29
        audi                  a4   1.8 1999   4 manual(m5)   f  21  29
        audi                  a4   2.0 2008   4 manual(m6)   f  20  31
        audi                  a4   2.0 2008   4   auto(av)   f  21  30
        audi                  a4   2.8 1999   6   auto(l5)   f  16  26
        audi                  a4   2.8 1999   6 manual(m5)   f  18  26
        audi                  a4   3.1 2008   6   auto(av)   f  18  27
        audi          a4 quattro   1.8 1999   4 manual(m5)   4  18  26
        audi          a4 quattro   1.8 1999   4   auto(l5)   4  16  25
        audi          a4 quattro   2.0 2008   4 manual(m6)   4  20  28
        audi          a4 quattro   2.0 2008   4   auto(s6)   4  19  27
        audi          a4 quattro   2.8 1999   6   auto(l5)   4  15  25
        audi          a4 quattro   2.8 1999   6 manual(m5)   4  17  25
        audi          a4 quattro   3.1 2008   6   auto(s6)   4  17  25
        audi          a4 quattro   3.1 2008   6 manual(m6)   4  15  25
        audi          a6 quattro   3.1 2008   6   auto(s6)   4  17  25

Since I didn't have access to the original data, here is an answer that illustrates how to write a file of make, model, and MPG for cars that have more than 20 MPG from the mtcars data set.

First, we have to extract the make and model from the row names, and clean up a few of the rows.

makeModel <- rownames(mtcars)
strings <- strsplit(makeModel," ")
# clean the data so each row has make and model
strings[[2]] <- c("Mazda","RX4 Wagon")
strings[[4]] <- c("AMC","Hornet 4 Drive")
strings[[5]] <- c("AMC","Hornet Sportabout")
strings[[6]] <- c("Plymouth","Valiant")
strings[[7]] <- c("Plymouth","Duster 360")
strings[[24]] <- c("Chevrolet","Camaro Z28")

Next, we add make , model and year to the data frame. Note that all the cars in mtcars are from the 1973 - 74 model year.

# add make and model to data frame
mtcars$make <- unlist(lapply(1:length(strings),function(x){strings[[x]][1]}))
mtcars$model <- unlist(lapply(1:length(strings),function(x){strings[[x]][2]}))
mtcars$year <- 1973

Now we write a function to read the data, subset on mpg > 20 and write only make , model , year and mpg to an output file, tab separated.

carpFunction <- function(x,outfile = "./mtcarsSubset.txt"){
     outputData <- x[x$mpg > 20,c(12:14,1)]
     write.table(outputData,file=outfile,sep="\t",row.names = FALSE)
}

We call the function to create the output file.

carpFunction(mtcars)

...and the output, when viewed in a text editor such as UltraEdit where we can see the tabs as » , looks like this.

"make"»"model"»"year"»"mpg"
"Mazda"»"RX4"»1973»21
"Mazda"»"RX4 Wagon"»1973»21
"Datsun"»"710"»1973»22.8
"AMC"»"Hornet 4 Drive"»1973»21.4
"Merc"»"240D"»1973»24.4
"Merc"»"230"»1973»22.8
"Fiat"»"128"»1973»32.4
"Honda"»"Civic"»1973»30.4
"Honda"»"Corolla"»1973»33.9
"Honda"»"Corona"»1973»21.5
"Fiat"»"X1-9"»1973»27.3
"Porsche"»"914-2"»1973»26
"Lotus"»"Europa"»1973»30.4
"Volvo"»"142E"»1973»21.4

Note, now that the OP has posted data we still shouldn't provide a complete answer for homework questions. The techniques used in this answer can be used to answer the homework question.

To demonstrate this, we'll provide everything except the code within the carPFunction() function.

textFile <- "manufacturer|model |displ |year |cyl |trans |drv |cty|hwy 
audi|a4|1.8|1999|4|auto(l5)  |f|18|29         
audi|a4|1.8|1999|4|manual(m5)|f|21|29         
audi|a4|2.0|2008|4|manual(m6)|f|20|31         
audi|a4|2.0|2008|4|auto(av)  |f|21|30         
audi|a4|2.8|1999|6|auto(l5)  |f|16|26         
audi|a4|2.8|1999|6|manual(m5)|f|18|26         
audi|a4|3.1|2008|6|auto(av)  |f|18|27         
audi|a4 quattro|1.8|1999|4|manual(m5)|4|18|26 
audi|a4 quattro|1.8|1999|4|auto(l5)  |4|16|25 
audi|a4 quattro|2.0|2008|4|manual(m6)|4|20|28 
audi|a4 quattro|2.0|2008|4|auto(s6)  |4|19|27 
audi|a4 quattro|2.8|1999|6|auto(l5)  |4|15|25 
audi|a4 quattro|2.8|1999|6|manual(m5)|4|17|25 
audi|a4 quattro|3.1|2008|6|auto(s6)  |4|17|25 
audi|a4 quattro|3.1|2008|6|manual(m6)|4|15|25 
audi|a6 quattro|3.1|2008|6|auto(s6)  |4|17|25 
"
cars <- read.csv(text=textFile,sep="|",stringsAsFactors=FALSE)

carPFunction <- function(x,outfile = "./carPSubset.txt"){
     # code to exactly solve homework 
     # goes here
}
carPFunction(cars)

... and the output with » as placeholder for a horizontal tab:

"manufacturer"»"model"»"year"
"audi"»"a4"»1999
"audi"»"a4"»2008

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM