Combining two variables into one single variable

I am trying to combine two sets of data into one set. I have tried to use c() but it will not work. I have also tried to use rbind() and merge() with no luck. The only command that has worked so far is cbind() but when I ask for the summary it gives me vector 1 and vector 2 instead of it being one single variable. I have also tried lm() and keep getting: lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
0 (non-NA) cases

HW3data$D7A[HW3data$D7A == 3]<-NA
HW3data$D7A[HW3data$D7A == 4]<-NA
HW3data$D7A[HW3data$D7A == 9]<-NA
HW3data$D7A[HW3data$D7A == 2]<-5
HW3data$D7B[HW3data$D7B == 1]<-4
HW3data$partyid<- cbind(HW3data$D7A, HW3data$D7B)
HW3data$partyid

This is what I currently have but am not having luck with it.

I see that your data frame HW3data has two numeric columns D7A and D7B. What result do you want when you combine these columns? Say D7A = 5 and D7B = 4. What will the combined result be that will be stored in the partyid column??

The two combined into one should create a list of numbers in the range of 1-5 (for a scale). So it should show people's responses from a survey

I am still not sure I understand your goal. In the code below, I invented a little data frame with two columns named D7A and D7B and then I made a new column that is a list column. Each row in that column contains a vector of length two the contains the two values from D7A and D7B. Is that what you want?

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
HW3data <- data.frame(D7A=sample(1:5,6,replace = TRUE),
                      D7B=sample(1:5,6,replace = TRUE))
HW3data
#>   D7A D7B
#> 1   4   1
#> 2   3   2
#> 3   2   5
#> 4   2   5
#> 5   3   5
#> 6   1   3
HW3data <- HW3data |> mutate(partyid=map2(D7A,D7B,c))
HW3data
#>   D7A D7B partyid
#> 1   4   1    4, 1
#> 2   3   2    3, 2
#> 3   2   5    2, 5
#> 4   2   5    2, 5
#> 5   3   5    3, 5
#> 6   1   3    1, 3
HW3data$partyid
#> [[1]]
#> [1] 4 1
#> 
#> [[2]]
#> [1] 3 2
#> 
#> [[3]]
#> [1] 2 5
#> 
#> [[4]]
#> [1] 2 5
#> 
#> [[5]]
#> [1] 3 5
#> 
#> [[6]]
#> [1] 1 3

Created on 2022-04-05 by the reprex package (v2.0.1)

Not quite, from what I am understanding, the two lists of variables just need to be combined to make one long list of variables

You can make a standalone vector of D7A and D7B combined like this

AllDat <- c(HW3data$D7A, HW3data$D7B)

but that cannot be stored in the original data frame because it would have twice as many rows as the other columns.

yeah that is the issue that I am running into

Well, some part of the requirement has to be set aside. You can have a list column in the original data frame as in my first response or a separate vector. Why are you trying to make this new column? What is the goal of the process?

the goal is to satisfy my professor

I suspect there is a miscommunication somewhere. The three ways I can think of to "combine" the data are the two I have shown or a mathematical combination like taking the mean or the sum.

I has that issue but I was resolved it by using merge()

This topic was automatically closed 21 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.