Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 986309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:18:01+00:00 2026-05-16T05:18:01+00:00

I am interested in (functional) vector manipulation in R . Specifically, what are R

  • 0

I am interested in (functional) vector manipulation in R. Specifically, what are R‘s equivalents to Perl’s map and grep?

The following Perl script greps the even array elements and multiplies them by 2:

@a1=(1..8); 
@a2 = map {$_ * 2} grep {$_ % 2 == 0} @a1;
print join(" ", @a2)
# 4 8 12 16

How can I do that in R? I got this far, using sapply for Perl’s map:

> a1 <- c(1:8)
> sapply(a1, function(x){x * 2})
[1]  2  4  6  8 10 12 14 16

Where can I read more about such functional array manipulations in R?

Also, is there a Perl to R phrase book, similar to the Perl Python Phrasebook?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:18:01+00:00Added an answer on May 16, 2026 at 5:18 am

    R has “grep”, but it works entirely different than what you’re used to. R has something much better built in: it has the ability to create array slices with a boolean expression:

    a1 <- c(1:8)
    a2 <- a1 [a1 %% 2 == 0]
    a2
    [1] 2 4 6 8
    

    For map, you can apply a function as you did above, but it’s much simpler to just write:

    a2 * 2
    [1]  4  8 12 16
    

    Or in one step:

    a1[a1 %% 2 == 0] * 2
    [1]  4  8 12 16
    

    I have never heard of a Perl to R phrase book, if you ever find one let me know! In general, R has less documentation than either perl or python, because it’s such a niche language.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm interested in making ghost preview elements of very complex DOM objects. While I
I don't know much about functional programming but am interested in learning Clojure. Are
I am just interested to know how, in a purely functional language, you can
I'm interested in using Functional MetaPost on Mac OS X: http://cryp.to/funcmp/ I'm looking for
I'm interested in building an uninitialized_vector container, which will be semantically identical to std::vector
I'm interested in reassigning factory functions for testing reasons within Go, to mitigate the
I'm interested if there is any function like array_map or array_walk from php. Don't
I'm interested in calculation of Improper Integral for a function. Particularly it's a Gauss
I'm using Python and PyGTK. I'm interested in running a certain function, which gets
Interested in upgrading JAVA VERSION from JAVA 1.5 to JAVA 1.6 [oracle@server301 /]$ java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.