Column data displaying incorrectly

Hey there!

I have combined two columns from an imported set of data into a new table in R. The two columns imported are both numeric. But when I combined them in a table, one of the columns has converted the values to A, B, C etc, and the header is incorrect, and in the other column the values are correct but the header is incorrect.


The first column should say 'height.parents' and the second column should say 'height'.

Any ideas what's happening? I added 'header=TRUE'.


Nope, no idea.

Seriously, remember that we don't have any context. Please show the lines of code that produced this, preferably by a Reproducible Example.

I'm really sorry about that. Here's the code... Note: my code is probably a big mess but I'm very new to R :') and trying to learn....

> setwd("/Users/N/Documents/R")
> cohort <- read.csv("data_2022_nas removed.csv", header=TRUE)
> library(tidyverse)
> library(measurements)
> #this function is the code that transforms inches to cm
> ft_inch <- function(str_ft_inch){
+   elem <- as.integer(unlist(strsplit(str_ft_inch, "'")))
+   inch <- elem[1]*12 + elem[2]
+   return(conv_unit(inch, "inch", "cm"))
+ }
> #now i'm going to transform the inch obs into cm. it worked!
> #thank you to user Flm on RStudio community.
> cohort$height <- sapply(cohort$height, ft_inch) %>% 
+   as_tibble() %>% 
+   mutate(inc = cohort$height,
+          value = ifelse(!, value, inc)) %>% 
+   select(value)
> #viewing the result
> View(cohort)
> #now im going to try and convert the other height columns
> cohort$ <- sapply(cohort$, ft_inch) %>% 
+   as_tibble() %>% 
+   mutate(inc = cohort$,
+          value = ifelse(!, value, inc)) %>% 
+   select(value)
> #and finally to mum
> cohort$height.mum <- sapply(cohort$height.mum, ft_inch) %>% 
+   as_tibble() %>% 
+   mutate(inc = cohort$height.mum,
+          value = ifelse(!, value, inc)) %>% 
+   select(value)
> #now converting to numeric
> cohort$height <- as.numeric (unlist(cohort$height))
> cohort$ <- as.numeric (unlist(cohort$
> cohort$height.mum <- as.numeric (unlist(cohort$height.mum))
> #creating a new column containing the mean of both parents' heights
> cohort$height.parents=rowMeans(cbind(cohort$,cohort$height.mum),na.rm=TRUE)
> #creating a new table called heights to tidy up
> heights <- as.table (cohort$height.parents, cohort$height, header=TRUE)
> #now creating a scatter plot
> my.regression <- plot(cohort$height, cohort$height.parents)
> is.numeric(cohort$height.parents)
[1] TRUE
> head(heights)
     A      B      C      D      E      F 
166.37 175.26 167.64 170.18 170.18 177.80

Ideas for how you can improve your post to increase your odds of getting help.
Don't include private objects that forum users have no access to. In your case you read in a csv into cohort
It would be wise to omit the reading of the csv and provide a sample of cohort using proven methods (such as dput()
Further, when providing code for consideration; do so from copying out of a script, rather than from out of a console you have run. Otherwise a forum user will have the unpleasant task of removing > from the start of each line and omitting unrunnable output lines such as [1] TRUE (i.e. not code)

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.