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

  • Home
  • SEARCH
  • 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 7612331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:53:01+00:00 2026-05-31T01:53:01+00:00

I need to make tutorial for beginner using the R *apply function (without using

  • 0

I need to make tutorial for beginner using the R *apply function (without using reshape or plyr package in a first time)

I try to lapply (because i read apply is not good for dataframe) a simple function to this dataframe, and i want to use named column to access data :

fDist <- function(x1,x2,y1,y2) {
  return (0.1*((x1 - x2)^2 + (y1-y2)^2)^0.5)  
}

data <- read.table(textConnection("X1 Y1 X2 Y2
 1 3.5 2.1 4.1 2.9
 2 3.1 1.2 0.8 4.3
 "))

data$dist <- lapply(data,function(df) {fDist(df$X1 , df$X2 , df$Y1 , df$Y2)})

I have this error $ operator is invalid for atomic vectors, it is probably because the dataframe is modified by laply ?… is there a best way to do that with $ named column?

I resolve my first question with @DWin answer. But i have another problem, misunderstanding, with mixed dataframe (numeric + character) :

In my new use case, i use two function to compute distance, because my objective is to compare a distance Point between all of other Point.

data2 <- read.table(textConnection("X1 Y1 X2 Y2
     1 3.5 2.1 4.1 2.9
     2 3.1 1.2 0.8 4.3
     "))

data2$char <- c("a","b")

fDist <- function(x1,y1,x2,y2) {
 return (0.1*((x1 - x2)^2 + (y1-y2)^2)^0.5) 
}

fDist2 <- function(fixedX,fixedY,vec) { 
 fDist(fixedX,fixedY,vec[['X2']],vec[['Y2']])
}

# works with data (dataframe without character), but not with data2 (dataframe with character)
#ok
data$f_dist <- apply(data, 1, function(df) {fDist2(data[1,]$X1,data[1,]$Y1,df)})
#not ok
data2$f_dist <- apply(data2, 1, function(df) {fDist2(data2[1,]$X1,data2[1,]$Y1,df)})
  • 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-31T01:53:02+00:00Added an answer on May 31, 2026 at 1:53 am

    In this case apply is what you need. All of the data columns are of the same type and you don’t have any worries about loosing attributes, which is where apply causes problems. You will need to write your function differently so it just takes one vector of length 4:

     fDist <- function(vec) {
       return (0.1*((vec[1] - vec[2])^2 + (vec[3]-vec[4])^2)^0.5)  
                            }
     data$f_dist <- apply(data, 1, fDist)
     data
       X1  Y1  X2  Y2    f_dist
    1 3.5 2.1 4.1 2.9 0.1843909
    2 3.1 1.2 0.8 4.3 0.3982462
    

    If you wanted to use the names of the columns in ‘data’ then they need to be spelled correctly:

     fDist <- function(vec) {
       return (0.1*((vec['X1'] - vec['X2'])^2 + (vec['Y1']-vec['Y2'])^2)^0.5)  
                            }
     data$f_dist <- apply(data, 1, fDist)
     data
    #--------    
    X1  Y1  X2  Y2    f_dist
    1 3.5 2.1 4.1 2.9 0.1000000
    2 3.1 1.2 0.8 4.3 0.3860052
    

    Your updated (and very different) question is easy to resolve. When you use apply it coerces to the lowest common mode denominator, in this case ‘character’. You have two choices: either 1) add as.numeric to all of your arguments inside the functions, or 2) only send the columns that are needed which I will illustrate:

    data2$f_dist <- apply(data2[ , c("X2", "Y2") ], 1, function(coords) 
                                           {fDist2(data2[1,]$X1,data2[1,]$Y1, coords)} )
    

    I really do not like how you are passing parameters to this function. Using “[” and “$” within the formals list “just looks wrong.” And you should know that “df” will not be a dataframe, but rather a vector. Because it’s not a dataframe (or a list) you should alter the function inside so that it uses “[” rather than “[[“. Since you only want two of the coordinates, then only pass the two (numeric) ones that you would be using.

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

Sidebar

Related Questions

We need to make our enterprise ASP.NET/NHibernate browser-based application able to function when connected
I am using the tutorial to make one simple app. And unlike the sample
I am developing an Android application using Phonegap . I need to make the
Is there, somewhere a tutorial on how to make an eclipse plugin. need it
I'm looking to make an ontology for a project. It's my first time doing
I'm using a script where I need to make multiple events to make a
I have a crystal report file I need make a tiny edit in. It
I need to make a WebCast presentation soon and need to do some whiteboarding
I need to make an application in .NET CF with different/single forms with a
I need to make an ArrayList of ArrayLists thread safe. I also cannot have

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.