Entradas


One day, Person X asked Person Y, “How do you get the values present in the data frame column in R language?” So, Person Y answered, “There are many ways to extract columns from the data frame.” So, he requested Person X to check this tutorial.

There are many ways to extract columns from the data frame. In this article, we will discuss two scenarios with their corresponding methods.

Now, we will see how to extract columns from a data frame. First, let’s create a data frame.

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#display the market dataframe

print(market)

Result:

You can see the market data frame here:

Let’s discuss them one by one.

Scenario 1: Extract Columns From the Data Frame by Column Name

In this scenario, we will see different methods to extract column/s from a data frame using column names. It returns the values present in the column in the form of a vector.

Method 1: $ Operator

The $ operator will be used to access the data present in a data frame column.

Syntax:

Where,

  1. The dataframe_object is the data frame.
  2. The column is the name of the column to be retrieved.

Example

In this example, we will extract market_name and market_type columns separately.

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#extract market_name column

print(market$market_name)

#extract market_type column

print(market$market_type)

Result:

We can see that the values present in market_name and market_type were returned.

Method 2: Specifying Column Names in a Vector

Here, we are specifying column names to be extracted inside a vector.

Syntax:

dataframe_object[,c(column,….)]

Where,

  1. The dataframe_object is the data frame.
  2. The column is the name of the column/s to be retrieved.

Example

In this example, we will extract “market_id”, “market_squarefeet”, and “market_place” columns at a time.

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#extract columns – «market_id»,»market_squarefeet» and «market_place»

print(market[ , c(«market_id», «market_squarefeet»,«market_place»)])

Result:

We can see that the columns: “market_id”, “market_squarefeet”, and “market_place” were returned.

Method 3: subset() With select()

In this case, we are using subset() with a select parameter to extract column names from the data frame. It takes two parameters. The first parameter is the data frame object, and the second parameter is the select() method. The column names through a vector are assigned to this method.

Syntax:

subset(dataframe_object,select=c(column,….))

Parameters:

  1. The dataframe_object is the data frame.
  2. The column is the name of the column/s to be retrieved via the select() method.

Example

In this example, we will extract “market_id”,”market_squarefeet” and “market_place” columns at a time using subset() with select parameter.

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#extract columns -«market_id»,»market_squarefeet» and «market_place»

print(subset(market,select= c(«market_id», «market_squarefeet»,«market_place»)) )

Result:

We can see that the columns: “market_id”, “market_squarefeet”, and “market_place” were returned.

Method 4: select()

The select() method takes column names to be extracted from the data frame and loaded into the dataframe object using the “%>%” operator. The select() method is available in the dplyr library. Therefore, we need to use this library.

Syntax:

dataframe_object %>% select(column,….))

Parameters:

  1. The dataframe_object is the data frame.
  2. The column is the name of the column/s to be retrieved.

Example

In this example, we will extract “market_id”,”market_squarefeet”, and “market_place” columns at a time using the select() method.

library(«dplyr»)

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#extract columns – «market_id»,»market_squarefeet», and «market_place»

print(market %>% select(«market_id», «market_squarefeet»,«market_place»))

Result:

We can see that the columns: “market_id”, “market_squarefeet”, and “market_place” were returned.

Scenario 2: Extract Columns From Data Frame by Column Indices

In this scenario, we will see different methods to extract column/s from a data frame using column index. It returns the values present in the column in the form of a vector. Index starts with 1.

Method 1: Specifying Column Indices in a Vector

Here, we are specifying column indices to be extracted inside a vector.

Syntax:

dataframe_object[,c(index,….)]

Where,

        1. The dataframe_object is the data frame.
        2. The index represents the column/s position to be retrieved.

Example

In this example, we will extract “market_id”,”market_squarefeet”, and “market_place” columns at a time.

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#extract columns – «market_id»,»market_squarefeet» and «market_place» using column indices

print(market[ , c(1,5,3)])

Result:

We can see that the columns – “market_id”,”market_squarefeet” and “market_place” were returned.

Method 2: subset() With select()

In this case, we are using subset() with select parameters to extract columns from the data frame with column indices. It takes two parameters. The first parameter is the dataframe object and the second parameter is the select() method. The column indices through a vector are assigned to this method.

Syntax:

subset(dataframe_object,select=c(index,….))

Parameters:

  1. The dataframe_object is the data frame.
  2. The index represents the column/s position to be retrieved.

Example

In this example, we will extract “market_id”, “market_squarefeet”, and “market_place” columns at a time using the subset() method with select parameter.

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#extract columns – #extract columns – «market_id»,»market_squarefeet» and «market_place» using column indices

print(subset(market,select= c(1,5,3)) )

Result:

We can see that the columns: “market_id”, “market_squarefeet”, and “market_place” were returned.

Method 3: select()

The select() method takes the column indices to be extracted from the data frame and loaded into the data frame object using the “%>%” operator. The select() method is available in the dplyr library. Therefore, we need to use this library.

Syntax:

dataframe_object %>% select(index,….))

Parameters:

  1. The dataframe_object is the data frame.
  2. The index represents the column/s position to be retrieved.

Example

In this example, we will extract “market_id”,”market_squarefeet”, and “market_place” columns at a time using the select() method.

library(«dplyr»)

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#extract columns – #extract columns – «market_id»,»market_squarefeet» and «market_place» using column indices

print(market %>% select(1,5,3))

Result:

We can see that the columns: “market_id”, “market_squarefeet”, and “market_place” were returned.

Conclusion

This article discussed how we could extract the columns through column names and column indices using the select() and subset() methods with select parameters. And if we want to extract a single column, simply use the “$” operator.



Source link


Compressed files save on bandwidth when sending them to someone. You can compress any files, and there are different tools, such as zip and tar. The compressed files get extracted to the current working directory unless you specify a different one. Besides, the different utilities have various options that you must add to aid in extracting archive files to specific folders.

When using a decompressing tool, there is a way to specify a different directory for the extracted files. This guide will discuss how to create archive files and extract the contents to specific directories using unzip and tar in Linux.

Extracting zip Files

The zip files are created using zip, a cross-platform compression and packaging utility that allows specifying the compression levels, ranging from 1 to 9.

When using zip to create zip files, the extracted files are stored in the current directory. Let’s create zip files in the current directory, then extract the contents to a different location.

To create zip files, the syntax is:

$ zip [options] [zip-name] [zip-files]

In our case, we are compressing different files and folders. Our zip file name is example1.zip. The following command will be:

$ zip example1.zip *.txt *.bin names details

Our zip file is ready and is currently in the /Documents directory. If we were to extract it without specifying the path, the following command would be:

However, let’s specify the path and extract the file contents to /Downloads directory. Furthermore, you must add the -d flag to specify the path. Now, the syntax is:

$ unzip [zip-file] -d /path/directory

Create a directory to extract the zip file contents, then use unzip to extract the files using the following command:

$ mkdir -p ~/Downloads/zip-extracted
$ unzip example1.zip -d ~/Downloads/zip-extracted

If we list the contents of the created directory, we see that the extraction was a success.

That’s all to it. Whether you are working with a created or downloaded zip file, the process and concept are the same.

Extracting tar Archive Files

The tar format is the most common compression format. Most files are either tar.gz, tar, or tzg format. The extraction will work the same, and like zip files, the default extraction occurs in the current directory unless otherwise specified.

Quickly create a tar archive to use for the following example. In our case, our archive is example2.tar:

You can use the -C or —directory flags to extract the tar file. Also, you need to create the directory to hold the extracted files, as we did with unzip.

The syntax for the extraction is:

$ tar -xvf [tar-file] -C /path/directrory

or

$ tar -xvf [tar-file] –directory /path/directory

In our case, our commands will be:

$ mkdir -p ~/Downloads/tar-extracted
$ tar -xvf example2.tar -C ~/Downloads/tar-extracted

Note that example2.tar is the name of our tar archived file, and our path and directory to extract to is ~/Downloads/tar-extracted. Therefore, replace the names to match your case.

We can list and confirm if the extraction was a success, and the following output shows everything worked as expected:

The process is the same for other tar formats. For instance, to extract a .tgz file, the commands will be similar to those shown in the following image. Also, note that we are using the —directory flag, which is the same as -C.

Conclusion

The bottom line is that by default, extracting files on Linux stores the extracted files in the current working directory. You must specify the path if you need to use different directories to extract the files. Moreover, there are various options that you need to add when using different file extraction utilities. We’ve covered extraction using unzip and tar, the two common utilities you can use.



Source link


“FFMpeg is a free and open-source video and audio converter. It has been widely adopted by many applications, including VLC, the Android OS, Spotify, etc. ffmpeg provides unparallel features for working with audio and video files.

In this tutorial, we will focus on how to extract audio files from videos and other useful techniques.”

Installing FFMpeg

Before we can proceed, you need to ensure that you have the ffmpeg utility installed and available in your system.

Debian

1

$ sudo apt-get install ffmpeg

REHL

1
2
3

$ sudo yum install epel-release
$ sudo yum localinstall –nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
$ sudo yum install ffmpeg ffmpeg-devel

Arch/Manjaro

macOS

Keep in mind that ffmpeg may not be working depending on the system support.

You can verify that you have ffmpeg installed by running the command:

The command should return detailed information about your installed ffmpeg version.

FFMpeg Extract Audio From Video

Before we can extract an audio file from a video, we need to determine the audio version. We can do this by running the ffbrobe command followed by the path to the target video:

1

$ ffprobe BigBuckBunny.mp4

Replace BigBuckBunny.mp4 with the name of your target file.

Navigate to the end of the command output and check the audio stream information. You should see the audio version as:

From the output, we can see that the audio format is aac.

To extract the audio from the video without re-encoding, run the command:

1

ffmpeg -i BigBuckBunny.mp4 -vn -acodec copy BigBuckBunnyAudio.aac

In the command above, we use the -I flag to specify the input video. The -vn flags tell ffmpeg to strip the video stream from the output file. Finally, the -acodec copy tells ffmpeg to use the already existing audio stream.

FFMpeg Extract Audio From File – Method 2

You can use ffmpeg to convert a video file into mp3. Since an mp3 file cannot contain a video stream, ffmpeg will automatically strip it out.

The example command is as shown:

1

$ ffmpeg -i BigBuckBunny.mp4 BigBuckBunnyAudio.mp3

The command will create an audio file with the specified filename.

Extract Audio From Videos in a Directory

Suppose you want to extract videos from mp4 files in an entire directory.

On Windows, run the command below in your Command Prompt.

1

for %i in (*.mp4) do ffmpeg -i «%i» «%~i.mp3»

The command will locate all the mp4 files in the current directory and convert them into mp3 files with similar names.

On macOS and Linux, run the command:

1
2
3
4
5

for i in *.mp4;
  do name=`echo «$i« | cut -d‘.’ -f1`
  echo «$name«
  ffmpeg -i «$i« «${name}.mp3″
done

Extract Audio From Video With VBR

In some cases, you may want to extract audio from video with a variable bit rate. You can run the command:

1

$ ffmpeg -i BigBuckBunny.mp4 -map 0:0 -q:a 0 -acodec copy BigBuckBunny.aac

We use the -q:a 0 to extract audio with variable bitrate. The quality value can range from 0 to 9, with 0 representing the highest quality and 9 representing the lowest quality.

Extract Audio From Video With CBR

To extract an audio with a constant bitrate, run the command:

1

$ ffmpeg -i BigBuckBunny.mp4 -map 0:0 -b:a 320k -acodec copy BigBuckBunny.aac

In the command above, we use the -b:1 followed by the target bitrate value. In our case, we specify the audio with 320k bitrate.

Conclusion

In this article, you learned how to extract audio from video without encoding, batch processing videos to audio, extract audio with variable bitrate and extract audio with constant bitrate.

Thanks for reading!!



Source link