Issue while recoding multiple variables, error - in .subset(x, j) : invalid subscript type 'list'

Gigi39

I have a dataframe for a likert scale questionnarie. All of the questions have a 1-10 scale, but to run some analyses I want to recode the scale to be from 1 to 5.

So, I want to recode most of the variables of my data frame, excluding identification variables. I've tried several things and looked also into past threads but I cannot find a solution, I always get an error.

Since I'm new to R I believe it is just a rookie mistake but I hope you can help me.

Here is the code:

z <- data.frame (ID = c(23,24,25,26,27),
              Project = c("EA","EA","PLA","PLA","PLA"),
              Q1 = c(3,9,8,5,10),
              Q2 = c(1,2,6,7,9),
              Q3 = c(4,8,6,6,10))

recode_z <- z[,3:5] 

p3[,recode_p3] <- as.data.frame(lapply(p3[, recode_p3], function(x)ifelse(x == 2, 1), 
                                                                   ifelse(x == 3, 2), 
                                                                   ifelse(x== 4, 2),
                                                                   ifelse(x== 5, 3),
                                                                   ifelse(x== 6, 3),
                                                                   ifelse(x== 7, 4),
                                                                   ifelse(x== 8, 4),
                                                                   ifelse(x== 9, 5),
                                                                   ifelse(x== 10, 5,x)))

I get this error:

Error in .subset(x, j) : invalid subscript type 'list'

Can you spot the mistake? Thank you in advance!

Rui Barradas

Noting that the values are integers between 2 and 10 and that

(2:10) %/% 2
#[1] 1 1 2 2 3 3 4 4 5

therefore the new values are given by

(2:10 + 1) %/% 2
#[1] 1 2 2 3 3 4 4 5 5

here is a one-liner.

recode_z <- 3:5
z[recode_z] <- lapply(z[recode_z], function(x) (x + 1L) %/% 2L)

z
#  ID Project Q1 Q2 Q3
#1 23      EA  2  1  2
#2 24      EA  5  1  4
#3 25     PLA  4  3  3
#4 26     PLA  3  4  3
#5 27     PLA  5  5  5

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Issue while recoding multiple variables, error - in .subset(x, j) : invalid subscript type 'list'

Recoding two variables into a new variable

Getting invalid type comparison error

About a type specifier in NuSMV (error: invalid subrange)

PostgreSQL - Error: "invalid input syntax for type bytea"

operator-sdk : getting error-Hit an unsupported type invalid type for invalid type

Facing issue while inserting lookup type record in salesforce: mule esb

Memory pressure issue while downloading multiple files using AFNetworking

Error while type casting pointer to structure

Error while updating the database with multiple php files

check for multiple substring match in a list while iterating list

React native elements error : Invariant Violation : Element type is invalid

Reactjs Uncaught error : Element type is invalid : expected a string

Swift NSArray subscript a value of type AnyObject

Error while passing variables for keyword match using sed

반복되는 질문의 여러 플롯에서 "Error in exm [[dups [j]]] : subscript out of bounds"가 생성됩니다.

Error "Subscript out of bounds" in Shiny, but not in R?

"subscript out of bounds" error on my custom function

Runtime Error 9 - Subscript Out Of Range

Linked list node assignment unexpectedly updates multiple variables

Resulting in an `error: incomplete type` while using std::tuple_size

Issue with .on('error')

Getting error while runing multiple return statement from cypher query

MongoDB Atlas Error while performing transaction on multiple collections (code 8000)

TicTacToe invalid move method issue

Multiple if statements for multiple variables

error while connecting to hp Quality Center using com4j

Element type is invalid: expected a string (for built-in components) or a class/function React Error

Playframework Java enums type in form 's selects validate as an error.invalid with actual data

TOP 리스트

  1. 1

    어떻게 아무리 "나쁜", ANY의 SSL 인증서와 HttpClient를 사용하지합니다

  2. 2

    SAP Java Connector (SAP JCO) SAP Funcion Module의 동적 매개 변수 및 값 목록

  3. 3

    자바, 레디 스, 탄성 검색, 몽고와 자동 완성

  4. 4

    ElasticSearch-노드 잠금을 가져 오지 못했습니다.

  5. 5

    Excel : 합계가 N보다 크거나 같은 상위 값 찾기

  6. 6

    rclone으로 원격 디렉토리의 모든 파일을 삭제하는 방법은 무엇입니까?

  7. 7

    Ubuntu는 GUI에서 암호로 사용자를 만듭니다.

  8. 8

    R-이중 항목 피벗 테이블 만들기

  9. 9

    PyCharm에서 갑자기 일부 메뉴 명령이 작동을 멈췄습니다.

  10. 10

    Cassandra에서 버전이 지정된 계층의 효율적인 모델링

  11. 11

    자바의 .txt 파일에서 2D 배열 읽기

  12. 12

    매개 변수에서 쿼리 객체를 선언하는 방법은 무엇입니까?

  13. 13

    cypress로 배열의 인덱스를 가져 오면 대신 "-1"이 반환됩니다.

  14. 14

    VBA-조건에 따라 자동으로 팝업되는 오류 메시지를 가질 수 있습니까?

  15. 15

    여러 양식을 사용하여 목록 상자 내에서 데이터 편집 문제

  16. 16

    jfreecharts에서 x 및 y 축 선을 조정하는 방법

  17. 17

    스크립트에 의해 SSH를 통해 자동으로 X 프로세스 전달

  18. 18

    그룹 별 중복 관측치를 축소하면서 테이블 전치

  19. 19

    마우스 휠 JQuery 이벤트 핸들러에 대한 방향 가져 오기

  20. 20

    버튼을 클릭하기 위해 pywinauto를 가져올 수 없습니다.

  21. 21

    randomUUID ()를 사용하여 생성 된 UUID의 길이를 줄이는 방법

뜨겁다태그

보관